junitmockitoverify

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


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:

Solution

  • 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");