intermediate~4h

Advanced Mockito

Relaxing exact-value stubbing with argument matchers, proving a side effect actually happened with verify() and ArgumentCaptor, the deep architectural difference between a mock and a spy including the doReturn() trap, and the advanced toolkit of dynamic Answers, static mocking, and strict stubbing.

Learning objectives

  • Use any(), eq(), and argThat() correctly, including the all-or-nothing matcher rule
  • Verify interactions with verify(), InOrder, and capture outgoing arguments with ArgumentCaptor
  • Explain the mock-vs-spy architectural difference and why doReturn() replaces when() for spies
  • Use Answer for dynamic, computed mock responses instead of static canned values
  • Explain Mockito's strict stubbing and why unused stubs cause a test failure rather than a silent pass

This is a Pro chapter

Sign in, then upgrade to Pro or Power to unlock this and the full Core Java Mastery library.

Advanced Mockito

Next Step

Continue to Spring Boot Testing Layers →← Back to all Testing — JUnit, Mockito & Spring Boot chapters