Mockito.doAnswer 与单元测试中的 ArgumentCaptor 对比

Mockito.doAnswer vs ArgumentCaptor in Unit Tests

我是 JavaUnit Testing 的新手,并尝试使用 ArgumentCaptor 测试无效方法。另一方面,我看到还有另一种称为 Mockito.doAnswer 的方法也可用于测试 void 方法。

在这个场景中:

1.Mockito.doAnswerArgumentCaptor的主要用途是什么?

2.Mockito.doAnswerArgumentCaptor和pros/cons有什么区别?

它们是完全不同的东西。

  • DoAnswer 用于存根。用于设置您希望在调用特定方法时发生的“假”行为。
  • ArgumentCaptor用于验证。如果您想在测试的 运行 期间检查传递给方法的参数,可以使用它。