Mockito Nullable ArgumentCaptor Value



是否可以有一个可空的ArgumentCaptor?

我正在尝试使用下面的方法

public Win getWin() {
ArgumentCaptor<Win> winCaptor = ArgumentCaptor
.forClass(Win.class);
Mockito.verify(producer, Mockito.atMostOnce()).accept(winCaptor.capture());
return winCaptor.getValue();
}

这工作很好,当producer.accept(win)被调用,但我希望它返回null,如果它不是。这可能吗?

我现在得到这个错误

No argument value was captured!
You might have forgotten to use argument.capture() in verify()...
...or you used capture() in stubbing but stubbed method was not called.
Be aware that it is recommended to use capture() only with verify()

您可以使用winCaptor.getAllValues()方法。如果没有调用accept()方法,它不会抛出异常。相反,它将返回一个空列表。