HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaCritical

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howmockitocalledverifymethodusingnotspecificthatwas

Problem

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??**
    }
}

Solution

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.

Code Snippets

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

// ...

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

Context

Stack Overflow Q#12862659, score: 1579

Revisions (0)

No revisions yet.