Perhaps there is a simpler solution, since Mockito already gives you the opportunity to check the number of calls to a particular layout using, Mockito.verify()but I have not found any method to return this number so that you can use the answers and implement your own counter:
MyClass myObject = mock(MyClass.class);
final int counter = 0;
when(myObject.myMethod()).then(new Answer<Result>() {
@Override
public Result answer(InvocationOnMock invocation) throws Throwable {
counter++;
return result;
}
});
The problem with this solution is that you need to write above for each method that you are mocking.
Mokito 1. 10+:
In fact, after going through the API for the version, 1.10I found:
Mockito.mockingDetails(mock).getInvocations();
source
share