Mockito Test Setup JUnit 5 | Java Fresher Interview
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
class at the top of your test class.
This tells JUnit to let Mockito handle the mock lifecycle. Inside the class, you use @Mock to create fake versions of your dependencies—like repositories or external services. Then you use @InjectMocks on the class you're actually testing, and Mockito automatically injects those mocks into it.
For example, if I'm testing an OrderService that needs a PaymentGateway and an InventoryRepository, I'd mark both of those as @Mock and the OrderService as @InjectMocks. When the test runs, MockitoExtension initializes all the mocks before my test method starts, so everything's wired up and ready to go.
One thing freshers often miss—if you forget the @ExtendWith annotation, your mocks will be null and you'll get a NullPointerException the moment you try to use them. The extension is what actually creates the mock objects; without it, the annotations are just decorations that do nothing. That's the core setup—those three annotations in the right places, and you've got a working Mockito test.
Mockito Test Setup JUnit 5 | Java Fresher Interview
Direct Answer
Add @ExtendWith(MockitoExtension.class) to the test class, use @Mock to declare mock dependencies, and @InjectMocks for the class under test. MockitoExtension initializes all annotated mocks before each test method runs automatically.
Code Example
@ExtendWith(MockitoExtension.class)
class OrderServiceTest {
@Mock
private PaymentGateway paymentGateway;
@Mock
private InventoryRepository inventoryRepository;
@InjectMocks
private OrderService orderService; // Mockito injects the two mocks above
@Test
void shouldProcessOrderWhenPaymentSucceeds() {
var order = new Order(1L, "Laptop", 999.99);
when(paymentGateway.charge(999.99)).thenReturn(true); // stub behavior
when(inventoryRepository.reserve("Laptop")).thenReturn(true);
boolean result = orderService.processOrder(order);
assertTrue(result);
verify(paymentGateway).charge(999.99); // verify interaction
verify(inventoryRepository).reserve("Laptop");
}
}
Common Mistakes & Pitfalls
- Forgetting @ExtendWith: Writing @Mock and @InjectMocks but not adding @ExtendWith(MockitoExtension.class) to the class header—mocks stay null and you get NullPointerException on the first method call.
- Wrong JUnit version: Using MockitoJUnitRunner (JUnit 4 syntax) in a JUnit 5 project—it doesn't work and fails at runtime with cryptic errors.
- Manual initialization confusion: Calling MockitoAnnotations.openMocks(this) in @BeforeEach when you already have @ExtendWith—you're initializing mocks twice, which is redundant and can cause subtle test issues.
Check your understanding
Answer these without looking back at the article.
- What is strict stubbing mode in Mockito?
- How do you reset mocks between tests?
- What is the difference between @ExtendWith and MockitoAnnotations.openMocks()?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.