Mockito可以捕获多次调用方法的参数吗?

问题:

我有一个方法被调用两次,我想捕获第二个方法调用的参数。
这是我试过的:

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());

 
 
Code问答: http://codewenda.com/topics/python/
Stackoverflow: Can Mockito capture arguments of a method called multiple times?

*转载请注明本文链接以及stackoverflow的英文链接

发表评论

电子邮件地址不会被公开。 必填项已用*标注

96 − 92 =