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?
Mockito.verify(mock, times(4)).send()
. I wonder why you "moved" the send()
method call inside the verify
method. You already had the right syntax.
verify(mock, atLeastOnce()).send();
Mockito.verify(mock).send()
passed but just encapsulating it caused an error, which, changes nothing. however, it's a win!
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: medium.com/javarevisited/…
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.
Success story sharing
VerificationMode
methods are (for static import or explicit reference), they are inorg.mockito.internal.verification.VerificationModeFactory
.verify(mock, atLeast(0)).someMethod("was called any number of times");
was helpful to ignore a call verificationverify(between(m,n))
which verifies number of calls between m and n?verify
to times withatLeast(M)
andatMost(n)
to get the same behaviour. I've edited the answer the explain that.VerificationModeFactory
. It is still available in the newest version, but I agree that internal classes shouldn't be used.