intermediate~4h
Mockito Fundamentals
Why real classes almost never test a service in isolation on their own, the five classic kinds of test doubles that stand in for real collaborators, how Mockito wires @Mock fields into @InjectMocks under JUnit 5's MockitoExtension, and how to make a mock respond with when()/thenReturn().
Learning objectives
- Explain why testing a service against real collaborators (databases, payment gateways) makes unit tests slow and unreliable
- Distinguish the five classic test double types: Dummy, Stub, Fake, Mock, and Spy
- Activate Mockito in JUnit 5 via @ExtendWith(MockitoExtension.class) and understand what it does automatically
- Explain how @InjectMocks wires @Mock fields into the system under test, including its injection strategies
- Use when().thenReturn() to control a mock's return value for a given method call
This is a Pro chapter
Sign in, then upgrade to Pro or Power to unlock this and the full Core Java Mastery library.
Mockito Fundamentals