使用 ArgumentCaptor 验证调用参数
ArgumentCaptor 将接收已传递给方法的实际调用参数。
ArgumentCaptor<Foo> captor = ArgumentCaptor.forClass(Foo.class);
verify(mockObj).doSomethind(captor.capture());
Foo invocationArg = captor.getValue();
//do any assertions on invocationArg
对于多次调用 mocked 方法来接收所有调用参数的情况
List<Foo> invocationArgs = captor.getAllValues();
相同的方法用于捕获 varargs。
还有可能使用 @Captor 注释创建 ArgumentCaptor:
@Captor
private ArgumentCaptor<Foo> captor;