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

Catturare gli argomenti con ArgumentCaptor

Usi ArgumentCaptor di Mockito per catturare e verificare gli oggetti esatti passati a un mock negli scenari complessi.

Catturare gli argomenti con ArgumentCaptor è una lezione Testing Mastery: JUnit, Mockito & Integration Tests gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Testing Mastery: JUnit, Mockito & Integration Tests, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Testing Mastery: JUnit, Mockito & Integration Tests include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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

Domande Frequenti

La lezione «Catturare gli argomenti con ArgumentCaptor» è gratuita?

Sì — il testo completo di «Catturare gli argomenti con ArgumentCaptor» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Testing Mastery: JUnit, Mockito & Integration Tests, passa a CoddyKit PRO. Il corso Testing Mastery: JUnit, Mockito & Integration Tests include 4 lezioni in totale.

Cosa imparerò in «Catturare gli argomenti con ArgumentCaptor»?

Usi ArgumentCaptor di Mockito per catturare e verificare gli oggetti esatti passati a un mock negli scenari complessi. Eserciti Testing Mastery: JUnit, Mockito & Integration Tests con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Testing Mastery: JUnit, Mockito & Integration Tests?

Non è richiesta alcuna esperienza precedente. Testing Mastery: JUnit, Mockito & Integration Tests su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Catturare gli argomenti con ArgumentCaptor»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Testing Mastery: JUnit, Mockito & Integration Tests?

Sì. Ogni lezione Testing Mastery: JUnit, Mockito & Integration Tests include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Risposte e callback personalizzati
  2. Mock di metodi statici e costruttori
  3. Buone pratiche per Mockito
  4. Catturare gli argomenti con ArgumentCaptor
← Torna a Testing Mastery: JUnit, Mockito & Integration Tests