Mockito Verify methods are used to check that certain behavior happened. We can use Mockito verify methods at the end of the testing method code to make sure that specified methods are called.
verifyNoMoreInteractions()
after all the verify() method calls to make sure everything is verified. If any method verification is still left, it will fail and provide proper message.verifyZeroInteractions()
behavior is same as verifyNoMoreInteractions()
method.inOrder()
method to verify the order of method invocation. We can skip a method invocation but the methods being verified must be in the same order.Let’s look at some of the mockito verify method examples.
@Test
void test() {
List<String> mockList = mock(List.class);
mockList.add("Pankaj");
mockList.size();
verify(mockList).add("Pankaj");
}
Above verify method will pass if add("Pankaj")
is called only once on the mocked list object. It’s the same as calling with times(1)
argument with verify method.
verify(mockList, times(1)).size();
If we want to make sure a method is called but we don’t care about the argument, then we can use ArgumentMatchers with verify method.
verify(mockList).add(anyString());
verify(mockList).add(any(String.class));
verify(mockList).add(ArgumentMatchers.any(String.class));
Note that org.mockito.Mockito
class provides static methods for most of the useful methods in the Mockito framework, this helps us in writing fluent code by importing them using import static
.
Mockito verify() method is overloaded, the second one is verify(T mock, VerificationMode mode)
. We can use it to verify for the invocation count.
verify(mockList, times(1)).size(); //same as normal verify method
verify(mockList, atLeastOnce()).size(); // must be called at least once
verify(mockList, atMost(2)).size(); // must be called at most 2 times
verify(mockList, atLeast(1)).size(); // must be called at least once
verify(mockList, never()).clear(); // must never be called
This method can be used after all the verify methods to make sure that all the interactions are verified. It will fail the test if there are any unverified interactions on the mocked object.
// all interactions are verified, so below will pass
verifyNoMoreInteractions(mockList);
mockList.isEmpty();
// isEmpty() is not verified, so below will fail
verifyNoMoreInteractions(mockList);
The second invocation of verifyNoMoreInteractions() will fail with the error message as:
org.mockito.exceptions.verification.NoInteractionsWanted:
No interactions wanted here:
-> at com.journaldev.mockito.verify.MockitoVerifyExamples.test(MockitoVerifyExamples.java:36)
But found this interaction on mock 'list':
-> at com.journaldev.mockito.verify.MockitoVerifyExamples.test(MockitoVerifyExamples.java:34)
***
For your reference, here is the list of all invocations ([?] - means unverified).
1. -> at com.journaldev.mockito.verify.MockitoVerifyExamples.test(MockitoVerifyExamples.java:18)
2. -> at com.journaldev.mockito.verify.MockitoVerifyExamples.test(MockitoVerifyExamples.java:19)
3. [?]-> at com.journaldev.mockito.verify.MockitoVerifyExamples.test(MockitoVerifyExamples.java:34)
One of the great features of Mockito is the exception message, it clearly points out where our test is failing so that we can easily fix it.
verifyZeroInteractions()
method behavior is same as verifyNoMoreInteractions() method.
Map mockMap = mock(Map.class);
Set mockSet = mock(Set.class);
verify(mockList).isEmpty();
verifyZeroInteractions(mockList, mockMap, mockSet);
If we want to verify that only one method is being called, then we can use only()
with verify method.
Map mockMap = mock(Map.class);
mockMap.isEmpty();
verify(mockMap, only()).isEmpty();
We can use InOrder
to verify the order of invocation. We can skip any method to verify, but the methods being verified must be invoked in the same order.
InOrder inOrder = inOrder(mockList, mockMap);
inOrder.verify(mockList).add("Pankaj");
inOrder.verify(mockList, calls(1)).size();
inOrder.verify(mockList).isEmpty();
inOrder.verify(mockMap).isEmpty();
Mockito verify() methods can be used to make sure the mock object methods are being called. If any method call is deleted by mistake, then verify method will throw an error.
You can look at more Mockito examples from our GitHub Repository.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Ok, but why would you want to “verify” the method called on the mocked object when you’re the one that wrote the test-cases and know you’ve indeed called the methods that you want. You can also see what methods are called, so why use verify?
- Swapnil Ingle