How to get the number of times a layout is called in Mockito

I use PowerMockito with Mockito to mock several static classes. I want to get the number of times a particular mock is called at runtime, so I can use this counter to check the time for another layout.

I need this because the method I'm testing starts a thread and stops the thread after a second. My bullying is called several times in this 1 second. After the first layout is called, you can call the code branches and various layouts. So, I want to compare the score of the first layout with the number of other layouts.

This is deprecated code. Therefore, I cannot make changes to the actual code. I can only change the test code.

+7
source share
1 answer

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();
+6
source

All Articles