Verify method was called n-times with Mockito

This short article will explain how to verify that Mock’ method was called n-times with the help of Mockito testing framework.

Introduction

We often encounter the problem during testing that we do not care about the data correctness, as much as we care about correct algorithm flow. In this situations, we want to check if specific methods were hit and executed. Sometimes we need to go deeper and seek an answer on how often these methods were called or verify that the methods called at all.

The necessary method, which will help us in our testing quest, is called verify() and is part of Mockito testing framework.

Mockito verify verification options

public static  T verify(T mock, VerificationMode mode)

mock is object of type T you are placing into the verify() method for verification. As a second method argument is VerificationMode mode variable, which describes how the mock should be verified. Possible verification modes are:

verify(mock, times(5)).someMethod("was called exactly five times");
verify(mock, never()).someMethod("was never called");
verify(mock, atLeast(5)).someMethod("was called at least five times");
verify(mock, atLeastOnce()).someMethod("was called at least once");
verify(mock, atMost(5)).someMethod("was called at most five times");

There is also one special VerificationMode mode called only(). This mode verifies that no other method was called on the mock so far.

verify(mock, only()).someMethod("only someMethod was called");

Mockito verify verification modes in action

In order to test individual verification modes, we will write a simple test. For testing purposes it will consist from two classes. (Both are inner private classes for MockitoVerifyTest.java.) First will be a fake class Account which trough dependency injection will use second class Counter. Counter class will be used as a Mock in our test.

Here is a Counter class:

private class Counter {

    private int counter;

    public Counter() {
        this.counter = 0;
    }

    public void increment() {
        this.counter++;
    }

    public void reset() {
        this.counter = 0;
    }

    public int getCount() {
        return this.counter;
    }
}

And here is the Account class:

private class Account {

    private Counter counter;

    public Account(Counter counter) {
        this.counter = counter;
    }

    public void incrementCounter() {
        this.counter.increment();
    }

    public void resetCounter() {
        this.counter.reset();
    }

    public int getCounterValue() {
        return this.counter.getCount();
    }
}

And finally, here is an exemplary test where you can see usage of individual verification modes:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.atMost;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class MockitoVerifyTest {

    @Mock
    Counter counter;

    @Test
    public void testMockitoVerifyMethodCalls() {

        Account account = new Account(counter);

        // Verification using only() and atLeastOnce()
        account.incrementCounter();
        Mockito.verify(counter, only()).increment();
        Mockito.verify(counter, atLeastOnce()).increment();

        // Verification using times(X)
        for (int i = 0; i < 4; i++) {
            account.incrementCounter();
        }
        Mockito.verify(counter, times(5)).increment();

        // Verification using atLeast(X), atMost(X), never()
        for (int i = 0; i < 5; i++) {
            account.incrementCounter();
        }
        Mockito.verify(counter, atLeast(5)).increment();
        Mockito.verify(counter, atMost(10)).increment();
        Mockito.verify(counter, never()).getCount();
    }
}

Let's discuss little but example code.

Account account = new Account(counter);

// Verification using only() and atLeastOnce()
account.incrementCounter();
Mockito.verify(counter, only()).increment();
Mockito.verify(counter, atLeastOnce()).increment();

First, we will create new instance of Account class which trough dependency injection is injected with Mock instance of our Counter class.

As first we verify that there was no method, except increment() method, has been called on our mock at all. And as second verification, we will check that it was called at least once,

for (int i = 0; i < 4; i++) {
    account.incrementCounter();
}
Mockito.verify(counter, times(5)).increment();

Now we call incrementCounter() method four more times, remember we already called it once, and we will check that it was mock method injected to instance was called exactly five times.

for (int i = 0; i < 5; i++) {
    account.incrementCounter();
}
Mockito.verify(counter, atLeast(5)).increment();
Mockito.verify(counter, atMost(10)).increment();
Mockito.verify(counter, never()).getCount();

In the last section, we are incrementing the counter again five more times, and now we are checking if it was called at least five times and at most ten times.

On the last line we are checking method call for Mock' getCount() method that was never called.

Conclusion

This article contains a straightforward test on which we have demonstrated how to use Mockito verify method for mock' method call verification.

As always, you can find all our examples on our GitHub project!

This entry was posted in Testing and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.