Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify mocked method not called with any combination of parameters using Mockito

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.

like image 711
Dan King Avatar asked Oct 10 '13 17:10

Dan King


People also ask

How do you know if mocked method called?

To check if a method was called on a mocked object you can use the Mockito. verify method: Mockito. verify(someMock).

Which method in Mockito verifies that no interaction has happened with a mock in Java?

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.

How verify method works in Mockito?

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.


1 Answers

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.

like image 168
Matt Lachman Avatar answered Sep 17 '22 17:09

Matt Lachman