@InjectMocks Injection Strategy | Java Fresher Interview
Fast recall
Quick revision
Core answer
@InjectMocks is a convenience, not a dependency-injection container. Mockito first considers the largest constructor and resolves arguments from declared mocks by type; unresolved non-mockable arguments may be passed as null. If constructor injection creates the object, Mockito does not continue with setter or field injection. Otherwise it may use a no-argument instance and try property/setter injection, then field injection. Unmatched dependencies can remain null without a clear injection error, while construction failures can still throw.Interview-ready guide
Interview answer
@InjectMocks is a Mockito convenience for creating the class under test and injecting declared mocks or spies. It is not a replacement for Spring or another dependency-injection container.
Mockito first considers the largest constructor and resolves its arguments from available mocks by type. If a non-mockable argument cannot be resolved, Mockito may pass null. Once constructor injection creates the object, it does not continue by filling other setters and fields. When constructor injection is not used, Mockito can create a no-argument instance, try property or setter injection, and finally try field injection. Names may help when more than one mock has the same type.
Because this is best effort, an unmatched collaborator can remain null without a specific “injection failed” message. Other problems, such as a constructor throwing or a type that cannot be instantiated, can produce a setup error. For a normal service with required constructor dependencies, manually calling new OrderService(repo, gateway) is often the clearest and safest test wiring.
@InjectMocks Injection Strategy | Java Fresher Interview
Direct Answer
@InjectMocks is a convenience, not a dependency-injection container. Mockito first considers the largest constructor and resolves arguments from declared mocks by type; unresolved non-mockable arguments may be passed as null. If constructor injection creates the object, Mockito does not continue with setter or field injection. Otherwise it may use a no-argument instance and try property/setter injection, then field injection. Unmatched dependencies can remain null without a clear injection error, while construction failures can still throw.
Code Example
class OrderService {
private final PaymentGateway paymentGateway;
private final InventoryRepository inventoryRepo;
private EmailSender emailSender; // optional dependency
// Constructor injection - Mockito tries this first
public OrderService(PaymentGateway paymentGateway, InventoryRepository inventoryRepo) {
this.paymentGateway = paymentGateway;
this.inventoryRepo = inventoryRepo;
}
// Setter injection - Mockito tries this second if constructor fails
public void setEmailSender(EmailSender emailSender) {
this.emailSender = emailSender;
}
}
@ExtendWith(MockitoExtension.class)
class OrderServiceTest {
@Mock PaymentGateway paymentGateway;
@Mock InventoryRepository inventoryRepo;
@Mock EmailSender emailSender;
@InjectMocks OrderService orderService; // wires all three via constructor + setter
@Test
void shouldProcessOrder() {
when(paymentGateway.charge(any())).thenReturn(true);
// test logic here
}
}
Common Mistakes & Pitfalls
- Expecting an Error on Injection Failure: Freshers assume @InjectMocks will throw an exception if a dependency can't be wired — it doesn't. It leaves the field null, causing cryptic NPE later.
- Mixing Field and Constructor Injection: Using @Autowired field injection in production code then wondering why @InjectMocks behaves unpredictably — the injection order becomes ambiguous.
- Not Using @ExtendWith(MockitoExtension.class): Forgetting this annotation means @Mock and @InjectMocks aren't processed, leaving everything null and causing test failures that look like mock issues but are actually initialization problems.
Check your understanding
Answer these without looking back at the article.
- When would you prefer manual construction over @InjectMocks?
- What are the limitations of field injection in production code?
- How do you debug a NullPointerException caused by @InjectMocks failure?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.