@@ -0,0 +1,2387 @@
+package io.github.walterinsh.mock;%0A%0Aimport org.testng.annotations.Test;%0A%0Aimport static org.mockito.Mockito.*;%0A%0A/**%0A * Usually, we want to know more about our mocked objects. The number of how many times our object has been invoked is%0A * important under some circumstances. It helps us to know what really happened underlying.%0A * Created by Walter on 12/2/15.%0A */%0Apublic class TrackInvocation %7B%0A%0A    /**%0A     * With Mockito.verify() and some special asserts(times, atLeast, atMost, never), we can reinforce our unit tests.%0A     * @throws Exception%0A     */%0A    @Test%0A    public void trackInvocationTimes() throws Exception %7B%0A        TargetClass targetClass = mock(TargetClass.class);%0A%0A        targetClass.targetMethod(null);%0A        targetClass.targetMethod(%22A%22);%0A        targetClass.targetMethod(%22A%22);%0A        targetClass.targetMethod(%22B%22);%0A%0A        verify(targetClass, times(2)).targetMethod(%22A%22);%0A        verify(targetClass, times(1)).targetMethod(%22B%22);%0A        verify(targetClass, times(4)).targetMethod(anyString());%0A%0A        verify(targetClass, atLeast(2)).targetMethod(%22A%22);%0A        verify(targetClass, atLeastOnce()).targetMethod(%22B%22);%0A%0A        verify(targetClass, atMost(1)).targetMethod(%22B%22);%0A%0A        verify(targetClass, never()).targetMethod(%22C%22);%0A    %7D%0A%0A    /**%0A     * If you want to ensure there are no more interactions(invocations) except what you need.%0A     * Mockito.verifyNoMoreInteractions might help.%0A     *%0A     * @throws Exception%0A     */%0A    @Test%0A    public void noMoreInvocations() throws Exception %7B%0A        TargetClass targetClass = mock(TargetClass.class);%0A%0A        // one interaction should happen%0A        targetClass.targetMethod(null);%0A%0A        /*%0A            The next commented code should not have happened. If you uncomment the code, the test fails,and with this%0A            friendly information%0A%0A            For your reference, here is the list of all invocations (%5B?%5D - means unverified).%0A            1. -%3E at io.github.walterinsh.mock.TrackInvocation.noMoreInvocations(TrackInvocation.java:50)%0A            2. %5B?%5D-%3E at io.github.walterinsh.mock.TrackInvocation.noMoreInvocations(TrackInvocation.java:60)%0A        */%0A        // targetClass.targetMethod(%22%22);%0A%0A        verify(targetClass, times(1)).targetMethod(null);%0A        verifyNoMoreInteractions(targetClass);%0A    %7D%0A%0A    class TargetClass %7B%0A%0A        public void targetMethod(String param) %7B%7D%0A    %7D%0A%7D%0A

add example show how to track invocation