Mockito Verify

Filed Under: Mockito

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.

Mockito Verify

  • Mockito verify() method can be used to test number of method invocations too. We can test exact number of times, at least once, at least, at most number of invocation times for a mocked method.
  • We can use 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.
  • We can use 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.

Mockito verify() simple example


@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 with number of times

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

verifyNoMoreInteractions()

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()

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);

Mockito verify only method call

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();

Mockito Verify Order of Invocation

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();

Summary

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages