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

void test() {
	List<String> mockList = mock(List.class);

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.


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


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
// isEmpty() is not verified, so below will fail

The second invocation of verifyNoMoreInteractions() will fail with the error message as:

No interactions wanted here:
-> at com.journaldev.mockito.verify.MockitoVerifyExamples.test(
But found this interaction on mock 'list':
-> at com.journaldev.mockito.verify.MockitoVerifyExamples.test(
For your reference, here is the list of all invocations ([?] - means unverified).
1. -> at com.journaldev.mockito.verify.MockitoVerifyExamples.test(
2. -> at com.journaldev.mockito.verify.MockitoVerifyExamples.test(
3. [?]-> at com.journaldev.mockito.verify.MockitoVerifyExamples.test(

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);
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);
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, calls(1)).size();


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.


  1. Swapnil Ingle says:

    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?

    1. Eshan says:

      In practice, you’ll pass mocked objects in the constructor/functions of the class you’re testing which will make function calls on mock, and not like in this article, perform operations on it directly.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content