How to verify a method is called two times with mockito verify()

Ahmad Beg picture Ahmad Beg · Feb 15, 2013 · Viewed 341.3k times · Source

I want to verify if a method is called at least once through mockito verify. I used verify and it complains like this:

org.mockito.exceptions.verification.TooManyActualInvocations: 
Wanted 1 time:
But was 2 times. Undesired invocation:

Answer

Liosan picture Liosan · Feb 15, 2013

Using the appropriate VerificationMode:

import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

verify(mockObject, atLeast(2)).someMethod("was called at least twice");
verify(mockObject, times(3)).someMethod("was called exactly three times");