我有一个被调用两次的方法,并且我想捕获第二个方法调用的参数。
这是我尝试过的:
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
只能被调用一次。如何验证
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());