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:

  1. good one: use separate tests each f.bar() invocations , test them independently.
  2. not one: use mockito.reset(f) before second invocation. resets state of spied f; instance if have inserted mock call dothrow(new exception).when(f).bar(45), reset after reset() call. second verify works times(1).

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -