I'm working in a Project with a Service
class and some sort of a Client
that acts as a facade (don't know if it's the right term in the Design Patterns's world, but I'll try to make myself clear). Service
's methods can be very expensive as they may be communicating with one or more databases, long checkings and so on, so every Client
method should call one and only one Service
method.
Service
class structure is something like
public class Service {
public void serviceA(){...}
public SomeObject serviceB(){...}
// can grow in the future
}
And Client
should be something like
public class Client {
private Service myService; // Injected somehow
public void callServiceA() {
// some preparation
myService.serviceA();
// something else
}
public boolean callServiceB(){...}
}
And in the test class for Client
I want to have something like
public class ClientTest{
private Client client; // Injected or instantiated in @Before method
private Service serviceMock = mock(Service.class);
@Test
public void callServiceA_onlyCallsServiceA() {
client.callServiceA();
????
}
}
In the ????
section I want something like verifyOnly(serviceMock).serviceA()
saying "verify that serviceMock.serviceA()
was called only once and no other method from the Service
class was called". Is there something like that in Mockito or in some other mocking library? I don't want to use verify(serviceMock, never()).serviceXXX()
for every method because, as I said, Service
class may grow in the future and I will have to be adding verification to every test (not a happy task for me) so I need something more general.
Thanks in advance for your answers.
EDIT #1
The difference between this post and the possible duplicate is that the answer adds boiler plate code which is not desired in my case because it's a very big project and I must add as few code as posible.
Also, verifyNoMoreInteractions can be a good option even when it's discouraged for every test, no extra boiler plate code needed.
To sumarize, the possible duplicate didn't solved my problem.
There's another issue: I'm writing test for code made by another team, not following a TDD proccess myself, so my test should be extra defensive, as stated in this article quoted in the mockito documentation for verifyNoMoreInteractions. The methods I'm testing are often very longs so I need to check that the method under test calls ONLY the necesary services and no other (because they're expensive, as I said). Maybe verifyNoMoreInteractions
is good enough for now but I'd like to see something not being discouraged for every test by the very same API creator team!
Hope this helps to clarify my point and the problem. Best regards.