Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting an absolute count of calls to a mocked method #77

Closed
KenYN opened this issue Aug 28, 2014 · 2 comments
Closed

Getting an absolute count of calls to a mocked method #77

KenYN opened this issue Aug 28, 2014 · 2 comments
Assignees
Milestone

Comments

@KenYN
Copy link

KenYN commented Aug 28, 2014

We can do this:

verify(mock1, times(3)).thisMethod();
verify(mock2, times(3)).thatMethod();

But we cannot do something like this, as far as I can see:

verifyEquals(times(mock1.thisMethod()), times(mock2.thatMethod()));

That is, I would like to verify that two mocked methods are called the same unspecified number of times. The number of calls made to a method appears to be already available, so this would be just querying the value directly, not indirectly via times(), atLeast(), etc.

I also posed this question to StackOverflow, and one answer, on using an Answer, seems good; perhaps a standard Answer implementation CountsCalls or similar should be the solution?

TimvdLippe added a commit to TimvdLippe/mockito that referenced this issue Jul 31, 2015
In order to count the number of invocations,
InvocationsCounter can keep track of the number of invocations
using an AtomicInteger.
This AtomicInteger can then be used in Mockito.times(AtomicInteger)
or with Assert.assertEquals(int, int) in order to verify behaviour.
@ChristianSchwarz
Copy link
Contributor

You can use a custom VerificationMode to count the invocations, here you go:

public class InvocationCounter {

    public static <T> T countInvocations(T mock, AtomicInteger count) {
        return Mockito.verify(mock, new Counter(count));
    }

    private InvocationCounter(){}

    private static class Counter implements VerificationInOrderMode, VerificationMode {
        private final AtomicInteger count;

        private Counter(AtomicInteger count) {
            this.count = count;
        }

        public void verify(VerificationData data) {
            count.set(data.getAllInvocations().size());
        }

        public void verifyInOrder(VerificationDataInOrder data) {
            count.set(data.getAllInvocations().size());
        }

        @Override
        public VerificationMode description(String description) {
            return VerificationModeFactory.description(this, description);
        }

    }

}

And then use it like this (works also with void return types):

@Mock
private Function<String, Integer> callable;

AtomicInteger count= new AtomicInteger(); //here is the actual invocation count stored

countInvocations(callable,count).apply( anyString());

assertThat(count.get(),is(2));

@mockitoguy
Copy link
Member

Mockito.mockingDetails(mock).getInvocations() API can be used to get the invocations. It's coming with 2.0. I'm closing this ticket if that's ok!

@bric3 bric3 added this to the 2.0 milestone Aug 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants