How can I verify that a mocked method was not called at all - with any combination of parameters - using Mockito?
For example I have an object - myObject
- that is using a second, mocked object - myMockedOtherObject
- that has a method - someMethodOrOther(String parameter1, String parameter2)
.
I want to call myObject.myMethod()
and verify that someMethodOrOther()
doesn't get called - with any combination of parameters.
e.g.:
myObject.doSomeStuff(); verify(myMockedOtherObject, never()).someMethodOrOther();
Except I can't do that, because someMethodOrOther()
requires specific parameters to be supplied.
To check if a method was called on a mocked object you can use the Mockito. verify method: Mockito. verify(someMock).
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.
Mockito. verify() will just verify that an invocation was done on a mock during the method execution. It means that if the real implementation of the mocked class doesn't work as it should, the test is helpless. As a consequence, a class/method that you mock in a test have also to be unitary tested.
You can accomplish what you want with Mockito's argument matchers:
myObject.doSomeStuff(); verify(myMockedOtherObject, never()).someMethodOrOther( Mockito.anyString(), Mockito.anyString() );
You can make that a little less verbose with a static import like you have for verify
and never
.
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