Tutorial

Mockito Verify

Published on August 3, 2022
author

Pankaj

Mockito Verify

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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

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.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
December 10, 2020

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

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.