0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · 课时

使用 ArgumentCaptor 捕获参数

使用 Mockito 的 ArgumentCaptor 捕获传递给模拟对象的确切对象,并在复杂场景中对其进行断言。

使用 ArgumentCaptor 捕获参数 是 CoddyKit 上的免费 Testing Mastery: JUnit, Mockito & Integration Tests 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Testing Mastery: JUnit, Mockito & Integration Tests 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Testing Mastery: JUnit, Mockito & Integration Tests 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Inspecting What Was Passed

Sometimes verifying that a method was called is not enough. You need to assert on the actual argument the code constructed and passed. Mockito's ArgumentCaptor grabs that value for inspection.

Matchers vs Captors

Argument matchers like eq(...) check inputs during verification. A captor instead records the argument so you can run rich assertions afterward.

Creating a Captor

Declare a captor for the type you expect to capture.

ArgumentCaptor<User> captor =
    ArgumentCaptor.forClass(User.class);

Capturing During Verify

Pass the captor's capture() where the argument would go inside verify.

verify(repo).save(captor.capture());

Reading the Value

After verification, retrieve the captured object and assert on its fields.

User saved = captor.getValue();
assertEquals("Ada", saved.getName());

Full Example

Here we confirm the service builds the correct user before saving it.

service.register("Ada", "ada@x.io");
verify(repo).save(captor.capture());
assertEquals("ada@x.io",
    captor.getValue().getEmail());

Capturing Multiple Calls

If the method was called several times, use getAllValues() to get every captured argument in order.

verify(repo, times(2)).save(captor.capture());
List<User> all = captor.getAllValues();

The @Captor Annotation

You can declare captors as fields with @Captor when using the Mockito extension, reducing boilerplate.

@Captor
ArgumentCaptor<User> captor;

Captor or Matcher?

Use a matcher for simple equality checks at call time. Reach for a captor when the argument is complex, built internally, or needs multiple assertions.

Avoid Overuse

Captors couple tests to internal construction details. Prefer them for genuinely complex arguments, not for everything, to keep tests resilient.

Why It Matters

Captors let you verify the precise data your code hands to collaborators, catching subtle mapping or transformation bugs.

Quick Check

How do you retrieve the captured argument after verification?

Recap

You learned argument capturing:

  • ArgumentCaptor.forClass or @Captor creates a captor
  • Pass capture() inside verify
  • getValue() / getAllValues() read the captured data
  • Use captors for complex, internally built arguments

常见问题解答

「使用 ArgumentCaptor 捕获参数」课时是免费的吗?

是的 — 「使用 ArgumentCaptor 捕获参数」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Testing Mastery: JUnit, Mockito & Integration Tests 课程的其余内容,请升级到 CoddyKit PRO。 Testing Mastery: JUnit, Mockito & Integration Tests 课程共包含 4 节课。

「使用 ArgumentCaptor 捕获参数」这节课中我会学到什么?

使用 Mockito 的 ArgumentCaptor 捕获传递给模拟对象的确切对象,并在复杂场景中对其进行断言。 你通过在浏览器中直接运行的动手代码来练习 Testing Mastery: JUnit, Mockito & Integration Tests,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Testing Mastery: JUnit, Mockito & Integration Tests 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Testing Mastery: JUnit, Mockito & Integration Tests 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「使用 ArgumentCaptor 捕获参数」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Testing Mastery: JUnit, Mockito & Integration Tests 课中编写并运行代码吗?

能。每节 Testing Mastery: JUnit, Mockito & Integration Tests 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 自定义答案与回调
  2. 模拟静态方法与构造函数
  3. Mockito 最佳实践
  4. 使用 ArgumentCaptor 捕获参数
← 返回 Testing Mastery: JUnit, Mockito & Integration Tests