Nook
Nook

Reputation: 532

Ignoring method calls in Mockito

With Mockito, is it possible to ignore a method call to a mock?

E.g. for the mock rugger created with mock(MyRugger.class):

class Pepe {

  public void runner() {     
    rugger.doIt(); 
    rugger.flushIt();
    rugger.boilIt();     
  } 
}

I need only test runner() but avoid the method flushIt().

Upvotes: 1

Views: 13600

Answers (3)

user43685
user43685

Reputation: 1506

Use ignoreStub method. See documentation here:

http://static.javadoc.io/org.mockito/mockito-core/2.13.0/org/mockito/Mockito.html

Upvotes: 0

BennyFlint
BennyFlint

Reputation: 610

Mockito is nice and will not verify calls unless you ask it to. So if you don't want to verify flush() just verify the methods you care about:

verify(rugger).doIt();
verify(rugger).boilIt();

If you want to verify that flush() was NOT called, use:

verify(rugger, never()).flush();

Upvotes: 3

pickypg
pickypg

Reputation: 22342

To reset a mock in Mockito, simply call reset on it. Note the very real concern mentioned in the above link and the JavaDoc for reset stating that it may represent bad design.

This should generally be avoided, but there are times when you simply need to do this. The below is an example on how to use it, not a good example of when to use it.

Object value = mock(Object.class);

when(value.equals(null)).thenReturn(true);
assertTrue(value.equals(null));
verify(value).equals(null);

reset(value);

assertFalse(value.equals(null));
verify(value).equals(null);

Upvotes: 4

Related Questions