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??** } }
Mockito verifyZeroInteractions() method It verifies that no interaction has occurred on the given mocks. It also detects the invocations that have occurred before the test method, for example, in setup(), @Before method or the constructor.
Internally Mockito uses Point class's equals() method to compare object that has been passed to the method as an argument with object configured as expected in verify() method. If equals() is not overridden then java. lang.
Verify in Mockito simply means that you want to check if a certain method of a mock object has been called by specific number of times. When doing verification that a method was called exactly once, then we use: verify(mockObject).
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.
Use the second argument on the Mockito.verify
method, as in:
Mockito.verify(dependency, Mockito.times(0)).someMethod()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With