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()
.
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);
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();
Use ignoreStub method. See documentation here:
http://static.javadoc.io/org.mockito/mockito-core/2.13.0/org/mockito/Mockito.html
精彩评论