Sunday, July 28, 2019

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



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

Answer




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.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...