7.8 Interaction Verification (verify, ArgumentCaptor and Matchers) (Week 7 • JUnit 5 & Mockito)
1. Core Concept & Internal Working
Interaction Verification (verify, ArgumentCaptor and Matchers) (JUnit 5 & Mockito) covers `verify(kafkaTemplate).send(...)`, `verify(repo, never()).save(any())` on validation failure, `ArgumentCaptor<Order>` to inspect exact saved fields. In production software engineering, it is used to Proving `orderRepo.save(...)` was NEVER called when stock check failed and capturing the exact `Order` persisted on success. Mastering its underlying execution rules and edge-case boundaries lets you write reliable, maintainable code.
2. Real-World Engineering Use Case
Proving `orderRepo.save(...)` was NEVER called when stock check failed and capturing the exact `Order` persisted on success.
3. Granular Subtopics Covered
- `verify(kafkaTemplate).send(...)`: Core foundation of Interaction Verification (verify, ArgumentCaptor and Matchers): Proving `orderRepo.save(...)` was NEVER called when stock check failed and capturing the exact `Order` persisted on success.
- `verify(repo: Master '`verify(repo' inside Interaction Verification (verify, ArgumentCaptor and Matchers) across real-world inputs.
- Never()).save(any())` on validation failure: Master 'Never()).save(any())` on validation failure' inside Interaction Verification (verify, ArgumentCaptor and Matchers) across real-world inputs.
- `ArgumentCaptor<Order>` to inspect exact saved fields: Master '`ArgumentCaptor<Order>` to inspect exact saved fields' inside Interaction Verification (verify, ArgumentCaptor and Matchers) across real-world inputs.
- Production Pitfall Guard (Interaction Verification (verify, ArgumentCaptor and Matchers)): Prevent common defect: Mixing raw values and argument matchers (`verify(svc).call(eq(1L), anyString())` — if one arg uses a matcher, ALL args must use matchers!).
- Hands-On Verification (Interaction Verification (verify, ArgumentCaptor and Matchers)): Use `ArgumentCaptor<Product>` to assert normalized uppercase SKU and trimmed name before `save()` is called.
4. Reference Implementation
var captor = org.mockito.ArgumentCaptor.forClass(Product.class);
org.mockito.Mockito.verify(productRepository).save(captor.capture());
Assertions.assertEquals("SKU-101", captor.getValue().getSku());
5. Common Mistakes to Avoid
- Mixing raw values and argument matchers (`verify(svc).call(eq(1L), anyString())` — if one arg uses a matcher, ALL args must use matchers!).
- Skipping boundary, null/empty, or error-state verification when implementing Interaction Verification (verify, ArgumentCaptor and Matchers).
- Pushing untested Interaction Verification (verify, ArgumentCaptor and Matchers) changes without clean naming and Git commit context.
6. Hands-On Exercise
Use `ArgumentCaptor<Product>` to assert normalized uppercase SKU and trimmed name before `save()` is called.
7. Technical Interview Checkpoints
- Q: What problem does Interaction Verification (verify, ArgumentCaptor and Matchers) solve in JUnit 5 & Mockito, and what fails if it is misused?
Key Focus: Proving `orderRepo.save(...)` was NEVER called when stock check failed and capturing the exact `Order` persisted on success. Guard against: Mixing raw values and argument matchers (`verify(svc).call(eq(1L), anyString())` — if one arg uses a matcher, ALL args must use matchers!). - Q: Explain the internal working of Interaction Verification (verify, ArgumentCaptor and Matchers) (`verify(kafkaTemplate).send(...)`, `verify(repo, never()).save(any())` on validation failure, `ArgumentCaptor<Order>` to inspect exact saved fields).
Key Focus: Walk step-by-step through `verify(kafkaTemplate).send(...)`, `verify(repo, never()).save(any())` on validation failure, `ArgumentCaptor<Order>` to inspect exact saved fields with a concrete code example. - Q: How did you apply and verify Interaction Verification (verify, ArgumentCaptor and Matchers) in your hands-on lab?
Key Focus: Use `ArgumentCaptor<Product>` to assert normalized uppercase SKU and trimmed name before `save()` is called.