@InjectMocks Mockito Java | Fresher Interview
Fast recall
Quick revision
Core answer
@InjectMocks asks Mockito to create or reuse the object under test and inject declared mocks or spies. It first considers the largest constructor. If constructor injection creates the object, Mockito does not continue with setter and field injection; otherwise it may try setters and then fields. It is best-effort convenience, not a full dependency-injection container, so direct constructor wiring is often clearer.Interview-ready guide
Interview answer
@InjectMocks is a convenience for wiring the class under test from the mocks and spies declared in the test. Mockito first considers the largest constructor and resolves its parameters by type. If it creates the object through that constructor, it does not continue into setter and field injection. If constructor injection is not used, it can try setters and then fields. This is best-effort wiring, not a full dependency-injection container, so an unmatched collaborator can remain null. For a simple service I often prefer new OrderService(repository, gateway) because the compiler checks the constructor and the wiring is visible. @InjectMocks is useful when that convenience stays unambiguous.
@InjectMocks Mockito Java | Fresher Interview
Direct Answer
@InjectMocks asks Mockito to create or reuse the object under test and inject declared mocks or spies. It first considers the largest constructor. If constructor injection creates the object, Mockito does not continue with setter and field injection; otherwise it may try setters and then fields. It is best-effort convenience, not a full dependency-injection container, so direct constructor wiring is often clearer.
Code Example
@ExtendWith(MockitoExtension.class) // required to process annotations
class OrderServiceTest {
@Mock
private OrderRepository mockRepository;
@Mock
private EmailService mockEmailService;
@Spy
private PriceCalculator spyCalculator = new PriceCalculator();
@InjectMocks
private OrderService service; // Mockito creates this and injects mocks
@Test
void testPlaceOrder() {
var order = new Order(1L, "Laptop", 999.99);
when(mockRepository.save(any(Order.class))).thenReturn(order);
var result = service.placeOrder(order);
// service was automatically instantiated with all dependencies
assertEquals(1L, result.id());
verify(mockEmailService).sendConfirmation("Laptop"); // injected mock tracked interaction
verify(mockRepository).save(order);
}
}
// The class under test needs a constructor
class OrderService {
private final OrderRepository repository;
private final EmailService emailService;
private final PriceCalculator calculator;
// Mockito calls this constructor with the three injected mocks
public OrderService(OrderRepository repository, EmailService emailService, PriceCalculator calculator) {
this.repository = repository;
this.emailService = emailService;
this.calculator = calculator;
}
}
Common Mistakes & Pitfalls
- Forgetting @ExtendWith(MockitoExtension.class): Without this annotation on the test class, Mockito's annotation processing never runs. Your @Mock fields become null, and @InjectMocks doesn't create the object, causing NullPointerException on the first line of your test. Always pair these two annotations.
- Silent injection failure: If your class has no matching constructor, no setters, and private final fields, all three injection strategies fail. Mockito creates the object anyway with null dependencies. Your test compiles but crashes at runtime. Enable Mockito's strict mode or write a setup assertion checking dependencies aren't null.
- Mixing @InjectMocks with manual instantiation: Writing @InjectMocks private OrderService service = new OrderService(...) defeats the purpose. Remove the = new part and let Mockito handle it entirely.
Check your understanding
Answer these without looking back at the article.
- What are the limitations of @InjectMocks?
- Should you use @InjectMocks or manual construction?
- How do you handle @InjectMocks when the class has multiple constructors?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.