Verifying interactions between a service and its dependencies using Mockito involves several key steps and methods. Here's a detailed guide on how to achieve this:
1. Mocking Dependencies**
First, you need to create mock objects for the dependencies of the service you are testing. Mockito provides the `mock()` method for this purpose:
java
// Create mock objects
Dependency dependency = mock(Dependency.class);
2. Setting Up the Service**
Next, you need to set up the service under test by injecting the mock dependencies. This can be done manually or using Mockito's `@InjectMocks` annotation:
java
// Manual injection
Service service = new Service(dependency);
// Using @InjectMocks
@InjectMocks
Service service;
@Mock
Dependency dependency;
3. Stubbing Behavior (Optional)**
If necessary, you can stub the behavior of the mock dependencies using `when()` to define what should happen when certain methods are called:
java
when(dependency.someMethod()).thenReturn("some result");
4. Calling the Service Method**
Now, call the method on the service that you want to test. This will trigger interactions with the mock dependencies:
java
// Call the method on the service
service.doSomething();
5. Verifying Interactions**
Use Mockito's `verify()` method to check if the expected interactions occurred. You can verify that a method was called with specific arguments, or that it was called a certain number of times:
java
// Verify that a method was called with specific arguments
verify(dependency).someMethod("specific argument");
// Verify that a method was called a certain number of times
verify(dependency, times(2)).someMethod();
You can also use `atLeast()`, `atLeastOnce()`, and `atMost()` to verify the number of invocations:
java
verify(dependency, atLeastOnce()).someMethod();
verify(dependency, atLeast(2)).someMethod();
verify(dependency, atMost(3)).someMethod();
6. Verifying No Interactions**
To ensure that no other methods were called on a dependency, use `verifyNoMoreInteractions()` after verifying the expected calls:
java
verify(dependency).someMethod();
verifyNoMoreInteractions(dependency);
Alternatively, if you want to verify that a specific method was not called at all, use `never()`:
java
verify(dependency, never()).anotherMethod();
7. Capturing Arguments**
If you need to inspect the arguments passed to a method, use an `ArgumentCaptor`:
java
ArgumentCaptor captor = ArgumentCaptor.forClass(String.class);
verify(dependency).someMethod(captor.capture());
assertEquals("expected argument", captor.getValue());
Example Use Case
Here's a complete example that demonstrates these concepts:
java
@Test
public void testServiceInteraction() {
// Create mock dependency
Dependency dependency = mock(Dependency.class);
// Set up the service
Service service = new Service(dependency);
// Stub behavior if needed
when(dependency.someMethod()).thenReturn("result");
// Call the service method
service.doSomething();
// Verify interactions
verify(dependency).someMethod();
verify(dependency, times(1)).anotherMethod();
// Verify no other interactions
verifyNoMoreInteractions(dependency);
}
This approach ensures that your tests are robust and cover all expected interactions between the service and its dependencies.
Citations:[1] https://community.lambdatest.com/t/how-can-you-use-mockito-to-verify-that-a-specific-method-was-not-called-on-an-objects-dependency/31706
[2] https://www.digitalocean.com/community/tutorials/mockito-verify
[3] https://www.vogella.com/tutorials/Mockito/article.html
[4] https://www.dhiwise.com/post/mastering-unit-testing-a-complete-guide-to-mockito-verify
[5] https://www.baeldung.com/mockito-verify
[6] https://stackoverflow.com/questions/11192187/is-it-possible-to-verify-arbitrary-interaction-using-mockito-in-a-compact-way
[7] https://vkuzel.com/mocking-and-verifying-java-functional-interfaces-with-mockito
[8] https://www.digitalocean.com/community/tutorials/mockito-injectmocks-mocks-dependency-injection
[9] https://www.toptal.com/java/a-guide-to-everyday-mockito