我有一个被调用两次的方法,我想捕获第二个方法调用的参数。
这是我尝试过的:
ArgumentCaptor<Foo> firstFooCaptor = ArgumentCaptor.forClass(Foo.class); ArgumentCaptor<Foo> secondFooCaptor = ArgumentCaptor.forClass(Foo.class); verify(mockBar).doSomething(firstFooCaptor.capture()); verify(mockBar).doSomething(secondFooCaptor.capture()); // then do some assertions on secondFooCaptor.getValue()
但是我得到了一个TooManyActualInvocations例外,因为 Mockito 认为doSomething应该只调用一次。
TooManyActualInvocations
doSomething
如何验证第二次调用的参数doSomething?
我认为应该是
verify(mockBar, times(2)).doSomething(...)
来自mockito javadoc的示例:
ArgumentCaptor<Person> peopleCaptor = ArgumentCaptor.forClass(Person.class); verify(mock, times(2)).doSomething(peopleCaptor.capture()); List<Person> capturedPeople = peopleCaptor.getAllValues(); assertEquals("John", capturedPeople.get(0).getName()); assertEquals("Jane", capturedPeople.get(1).getName());