184

I'm trying to verify that a (void) method is being called inside of a DAO - I'm using a commit point that sends a list of results up to that point, resets the list and continues. Say I have 4 things in the list and I have a commit point of 1, I would expect the "send" method to be called 4 times. I can verify that the method gets called once by writing

Mockito.verify(mock).send()

it passes.. but I want to verify the number of times it was called. I would think that

Mockito.verify(mock.send(), times(4))

would be sufficient, but it says the parameters are not correct for verify.

Incidentally, if I change Mockito.verify(mock).send() to Mockito.verify(mock.send()) or Mockito.verify((mock).send()) I get the same error. Thoughts on this?

nbpeth
  • 2,967
  • 4
  • 24
  • 34
  • 11
    Try [`Mockito.verify(mock, times(4)).send()`](http://mockito.googlecode.com/svn/branches/1.7/javadoc/org/mockito/Mockito.html#verify%28T,%20org.mockito.internal.verification.api.VerificationMode%29). I wonder why you "moved" the `send()` method call inside the `verify` method. You already had the right syntax. – Tom Jan 05 '15 at 20:40
  • http://stackoverflow.com/questions/14889951/how-to-verify-a-method-is-called-two-times-with-mockito-verify – austin wernli Jan 05 '15 at 20:40
  • Also note that if you don't care how many times something is called, you can write `verify(mock, atLeastOnce()).send();` – Dawood ibn Kareem Jan 05 '15 at 20:48
  • Thank you, that's correct. What I found confounding was that `Mockito.verify(mock).send()` passed but just encapsulating it caused an error, which, changes nothing. however, it's a win! – nbpeth Jan 05 '15 at 20:49
  • `verify()` combined with `times()` will allow you to do what you want as already mentioned by other users. Here an article also explaining the mechanics in a bit more depth: https://medium.com/javarevisited/how-to-verify-that-void-methods-were-called-using-mockito-f439bfa347be – javing Oct 25 '21 at 20:28

1 Answers1

307

The necessary method is Mockito#verify:

public static <T> T verify(T mock,
                           VerificationMode mode)

mock is your mocked object and mode is the VerificationMode that describes how the mock should be verified. Possible modes are:

verify(mock, times(5)).someMethod("was called five times");
verify(mock, never()).someMethod("was never called");
verify(mock, atLeastOnce()).someMethod("was called at least once");
verify(mock, atLeast(2)).someMethod("was called at least twice");
verify(mock, atMost(3)).someMethod("was called at most 3 times");
verify(mock, atLeast(0)).someMethod("was called any number of times"); // useful with captors
verify(mock, only()).someMethod("no other method has been called on the mock");

You'll need these static imports from the Mockito class in order to use the verify method and these verification modes:

import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.atMost;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

So in your case the correct syntax will be:

Mockito.verify(mock, times(4)).send()

This verifies that the method send was called 4 times on the mocked object. It will fail if it was called less or more than 4 times.


If you just want to check, if the method has been called once, then you don't need to pass a VerificationMode. A simple

verify(mock).someMethod("was called once");

would be enough. It internally uses verify(mock, times(1)).someMethod("was called once");.


It is possible to have multiple verification calls on the same mock to achieve a "between" verification. Mockito doesn't support something like this verify(mock, between(4,6)).someMethod("was called between 4 and 6 times");, but we can write

verify(mock, atLeast(4)).someMethod("was called at least four times ...");
verify(mock, atMost(6)).someMethod("... and not more than six times");

instead, to get the same behaviour. The bounds are included, so the test case is green when the method was called 4, 5 or 6 times.

Tom
  • 16,842
  • 17
  • 45
  • 54
  • 1
    For anyone looking to find where the `VerificationMode` methods are (for static import or explicit reference), they are in `org.mockito.internal.verification.VerificationModeFactory`. – Steve Chambers Sep 28 '15 at 10:30
  • ```verify(mock, atLeast(0)).someMethod("was called any number of times");``` was helpful to ignore a call verification – tj-recess May 17 '17 at 00:41
  • 2
    is there something like `verify(between(m,n))` which verifies number of calls between m and n? – krackoder Mar 20 '19 at 18:00
  • 2
    @nishant No, Mockito doesn't seem to support that, but you can call `verify` to times with `atLeast(M)` and `atMost(n)` to get the same behaviour. I've edited the answer the explain that. – Tom Mar 25 '19 at 08:37
  • @KevinWelker You're right, I've removed the information about `VerificationModeFactory`. It is still available in the newest version, but I agree that internal classes shouldn't be used. – Tom Mar 25 '19 at 08:44