java - Mockito validates already verified invocations -
static class foo { public void bar(int i) {} } @test public void foo() { foo f = mockito.spy(new foo()); f.bar(42); mockito.verify(f, mockito.times(1)).bar(42); f.bar(42); mockito.verify(f, mockito.times(1)).bar(42); }
causes org.mockito.exceptions.verification.toomanyactualinvocations
(wanted 1 time, 2) on last line. running in debug shows, invocationmatcher
ignores fact first invocation verified. , not depend on witch matcher passed bar
. doing wrong, or bug of mockito?
there no bug. implementors of library thinks multiple invocations in single test method not best practice. there 2 choices overcome issue:
- good one: use separate tests each
f.bar()
invocations , test them independently. - not one: use
mockito.reset(f)
before second invocation. resets state of spiedf
; instance if have inserted mock calldothrow(new exception).when(f).bar(45)
, reset afterreset()
call. second verify workstimes(1)
.
Comments
Post a Comment