0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · レッスン

ArgumentCaptorによる引数の捕捉

MockitoのArgumentCaptorを使って、複雑なシナリオでモックへ渡された正確なオブジェクトを捕捉し、検証します。

「ArgumentCaptorによる引数の捕捉」はCoddyKit上の無料Testing Mastery: JUnit, Mockito & Integration Testsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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による引数の捕捉」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応の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を演習し、24時間対応の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. カスタムAnswerとコールバック
  2. 静的メソッドとコンストラクターのモック化
  3. Mockitoのベストプラクティス
  4. ArgumentCaptorによる引数の捕捉
← Testing Mastery: JUnit, Mockito & Integration Testsに戻る