In this short article, we are going to present a way to verify a method is called two times using the Mockito testing framework. Verifying several method calls are common assertions used in unit tests.
2. Using Mockito
Luckily, the Mockito framework is fully prepared for such verifications. It comes with dedicated methods that allow us to check the number of method calls:
times(x)- methods was called many times,
never()- method was never called,
atLeastOnce()- method was called at least once,
atLeast(x)- method was called at least x times,
atMost(x)- method was called at most x times.
These methods are used in
verify(...) as a second parameter for example:
To check if method was called exactly two times you need to use the
Mockito.verify(..., times(2)) method like in the following example:
In this article, we presented how to check if a method was called two times with Mockito library. We showed that Mockito allows multiple verifications of a certain behavior if it happened at least once, an exact number of times, or never.