If you use @RunWith(MockitoJUnitRunner.class)
or @ExtendWith(MockitoExtension.class)
, you will get an UnnecessaryStubbingException after running a test if you provided a mock that is not used.
You can avoid this by setting @MockitoSettings(strictness = Strictness.LENIENT)
if you want to.
But even with the UnnecessaryStubbingException, it's still better to do an actual verify inside your test for readability. Your 'verify' can also be stricter than your stub, e.g:
when(mock.method(anyCollection())).thenReturn(...)
...
mock.method(collection);
...
verify(mock).method(argThat(collection -> collection.size()==1));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…