How to verify that a specific method was not called using Mockito?

JavaUnit TestingJunitMockitoTdd

Java Problem Overview


How to verify that a method is not called on an object's dependency?

For example:

public interface Dependency {
    void someMethod();
}

public class Foo {
    public bar(final Dependency d) {
        ...
    }
}

With the Foo test:

public class FooTest {
    @Test
    public void dependencyIsNotCalled() {
        final Foo foo = new Foo(...);
        final Dependency dependency = mock(Dependency.class);
        foo.bar(dependency);
        **// verify here that someMethod was not called??**
    }
}

Java Solutions


Solution 1 - Java

Even more meaningful :

import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

// ...

verify(dependency, never()).someMethod();

The documentation of this feature is there §4 "Verifying exact number of invocations / at least x / never", and the never javadoc is here.

Solution 2 - Java

Use the second argument on the Mockito.verify method, as in:

Mockito.verify(dependency, Mockito.times(0)).someMethod()

Solution 3 - Java

As a more general pattern to follow, I tend to use an @After block in the test:

@After
public void after() {
    verifyNoMoreInteractions(<your mock1>, <your mock2>...);
}

Then the test is free to verify only what should be called.

Also, I found that I often forgot to check for "no interactions", only to later discover that things were being called that shouldn't have been.

So I find this pattern useful for catching all unexpected calls that haven't specifically been verified.

Solution 4 - Java

First of all: you should always import mockito static, this way the code will be much more readable (and intuitive):

import static org.mockito.Mockito.*;

There are actually many ways to achieve this, however it's (arguably) cleaner to use the

verify(yourMock, times(0)).someMethod();

method all over your tests, when on other Tests you use it to assert a certain amount of executions like this:

verify(yourMock, times(5)).someMethod();

Alternatives are:

verify(yourMock, never()).someMethod();

Alternatively - when you really want to make sure a certain mocked Object is actually NOT called at all - you can use:

verifyZeroInteractions(yourMock)

Please Note: verifyZeroInteractions(Object... mocks) is Deprecated. Since Version 3.0.1. The now recommended method is:

verifyNoInteractions(yourMock)

Solution 5 - Java

Both the verifyNoMoreInteractions() and verifyZeroInteractions() method internally have the same implementation as:

public static transient void verifyNoMoreInteractions(Object mocks[])
{
    MOCKITO_CORE.verifyNoMoreInteractions(mocks);
}

public static transient void verifyZeroInteractions(Object mocks[])
{
    MOCKITO_CORE.verifyNoMoreInteractions(mocks);
}

so we can use any one of them on mock object or array of mock objects to check that no methods have been called using mock objects.

Solution 6 - Java

Just as a suggestion, if you want to be more aligned at syntax level with Behavior-driven development style there is BDDMockito:

You could use:

then(dependency).should(never()).someMethod();

As an equivalent replacement of:

verify(dependency, never()).someMethod();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbeluchinView Question on Stackoverflow
Solution 1 - JavaBriceView Answer on Stackoverflow
Solution 2 - JavabeluchinView Answer on Stackoverflow
Solution 3 - JavaDavid LavenderView Answer on Stackoverflow
Solution 4 - Javafl0wView Answer on Stackoverflow
Solution 5 - JavaUjjwalView Answer on Stackoverflow
Solution 6 - JavaAlex BlascoView Answer on Stackoverflow