In this post, I will show how you can use Mockito verify to check that a method has or hasn't been called. The Mockito
enables you to verify that a method is never called or called any number of times, and even the order they are called in.
Verify a method is called n times
To verify that a method is called you can use the
verify method. The verify method accepts a mock and times instance.
The time instance is used to specify how often a method is supposed to be called. In the following example, mockito checks that
a mock is called exactly two times.
times(2) mockito knows that the method has to be called twice.
If you want to verify that a method is only called once you don't have to use a
times(1) instance. Mockito will assume
that you only want to have the method to be invoked once if you do not pass a times instance.
Without the time instance
verify(car, times(1)).startEngine(); have the same result
Verify a method called between boundaries
If you only that a method is supposed to be called at least or at most x times. you can use
atLeat instead of times.
In the following example, Mockito verifies that a method is called between 2 and 3 times.
Using both atLeast and atMost creates a boundary. You can also only use one of them to create a minimum or maximum number of times a method is supposed to be called.
Called between 1 and 0 times or more than once
Using verify you can also verify that a method is called between 0 and 1 times using atMostOnce(). Or you can use atLeastOnce if you only want to know a method is called at least once. In the following example, both methods are used to verify that the method has been called.
Verify a method is not called
You can also verify that the method has never been called by using never().
No interaction with a mock
If you do not want any interaction with your mock you can use verifyNoInteractions().
Verify order of method calls
You can use InOrder if the order of the method calls is important for your tests. You can pass one or more mock instances to the InOrder method. In the following example, the InOrder instance is used to verify that the methods have been called in the correct order.
This example showed how to verify the order of method calls with one mock, but you can also use multiple mock instances as is shown in the next section.
Verify order with multiple mocks
In the following example, the order of multiple mock instances and method calls is verified. Using the InOrder instance allows us to specify in which order which method from each mock has to be called.
InOrder works for one or multiple mock instances.
Verify with timeout
Using both verify and timeout allows you to test concurrent code. Using timeout is not recommended as there are better ways to test your concurrent code. Know that exists, but it's not supposed to be used often.
The timeout parameter has an int as parameter which is the total timeout in milliseconds.
In this post, we looked at all the different ways you can verify the behavior of your mock or spy instances. As you have seen there are multiple methods mockito provides somewhat of the same functionality. Because they somewhat overlap with each other it is good to make an informed decision on which verify method to use. If you want to know more about mockito's verification methods please take a look at the official documentation.