Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito - verify that none of the collaborator's methods is called

Tags:

I'm a beginner in Mockito.

The SUT, Foo, has one collaborator, Bar, and in one particular test case, Foo should not call any of the methods of Bar.

How do I go about verifying it?

like image 941
Tom Tucker Avatar asked Jan 26 '12 17:01

Tom Tucker


People also ask

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.

How do you mock another method in the same class which is being tested?

We can mock runInGround(String location) method inside the PersonTest class as shown below. Instead of using mock(class) here we need to use Mockito. spy() to mock the same class we are testing. Then we can mock the method we want as follows.


1 Answers

Try this:

verifyZeroInteractions(bar); 

As explained here.

like image 156
Tomasz Nurkiewicz Avatar answered Sep 30 '22 01:09

Tomasz Nurkiewicz