Java Testing Part 2: Mocks, Stubs, and Spies with Mockito 4
summary
Covers Mockito 4/5 usage with concrete examples: an HTTP Gateway + Astro service that groups astronauts by spacecraft, unit tests that use a mock Gateway to return a hard‑coded AstroResponse and verify getResponse() is called exactly once, and an integration test that hits the real API. Shows argument matchers like anyInt and custom ARG lambdas, uses doThrow for void methods, demonstrates Spring’s @MockBean injection, and explains inline mock maker for mocking static/final methods; all example tests in the repo run and pass.
AI-written summary · about
chapters (14)
- 0:00The role of testing
- 2:15Speaker introduction
- 4:42Real-world testing example
- 9:22Why use Mockito
- 11:51Core principles of mocking
- 15:08The Mockito workflow
- 17:10Mocks vs. stubs
- 18:55Understanding spies
- 21:19Spring and BDD approach
- 25:44Documentation and setup
- 30:07Mockito versions and configuration
- 34:03Advanced interaction testing
- 39:25Argument matchers
- 41:22Handling edge cases and void methods
related talks
58mMockito Features and Best Practices
30mMocks, Stubs, and Spies, Oh My
36mMocks, Stubs, and Spies, Oh My!
01h 01mMastering Java Testing With JUnit, Mockito, and AssertJ in IntelliJ IDEA
31mBeyond the Mockery- Why We Should Embrace Testing Without Mocking Frameworks
31mGreat tools for engineers: even cleaner tests with Mockito 2
38mDoublers in Tests. Wiremock vs Mockito
37mBeyond the Mockery: Why We Should Embrace Testing Without Mocking Frameworks
