mockito Getting started with mockito Verify method calls on mocked object


Example

To check if a method was called on a mocked object you can use the Mockito.verify method:

Mockito.verify(someMock).bla();

In this example, we assert that the method bla was called on the someMock mock object.

You can also check if a method was called with certain parameters:

Mockito.verify(someMock).bla("param 1");

If you would like to check that a method was not called, you can pass an additional VerificationMode parameter to verify:

Mockito.verify(someMock, Mockito.times(0)).bla();

This also works if you would like to check that this method was called more than once (in this case we check that the method bla was called 23 times):

Mockito.verify(someMock, Mockito.times(23)).bla();

These are more examples for the VerificationMode parameter, providing more control over the number of times a method should be called:

Mockito.verify(someMock, Mockito.never()).bla(); // same as Mockito.times(0)

Mockito.verify(someMock, Mockito.atLeast(3)).bla(); // min 3 calls

Mockito.verify(someMock, Mockito.atLeastOnce()).bla(); // same as Mockito.atLeast(1)

Mockito.verify(someMock, Mockito.atMost(3)).bla(); // max 3 calls