@InjectMocks tells Mockito to create an instance of the class under test and inject into it the mocks and spies declared in the test class with @Mock and @Spy. It removes the manual new UserService(repository) wiring from your test setup, so each test starts from a fully assembled object whose dependencies you control.
With JUnit 5 you activate it through @ExtendWith(MockitoExtension.class). Without the extension, the annotations do nothing until you call MockitoAnnotations.openMocks(this).
A unit test should exercise one class while its collaborators are replaced by test doubles. @Mock creates the doubles; @InjectMocks builds the real object and plugs the doubles into it.
MockitoExtension initialises the annotated fields before each test and, by default, uses strict stubs: an unused when(...) stubbing fails the test with UnnecessaryStubbingException, which keeps tests honest.
Mockito tries the following strategies, in this order, and stops at the first that applies.
null.If constructor injection is used, Mockito does not continue with setter or field injection. That is why a field left unset after a constructor call is a common surprise.
With two @Mock fields of those types, @InjectMocks UserService gets both through the constructor.
A @Spy wraps a real object so some methods run for real while others are stubbed. Spies are candidates for injection just like mocks.
When two fields share a type, for example two DataSource mocks, Mockito matches by field name. Name your mocks the same as the target field to make this deterministic.
openMocks: fields stay null and you get a NullPointerException.@InjectMocks needs a real, instantiable class. It cannot inject into a field typed as an interface without an implementation.null in the constructor. If you want failures to be visible, construct the object yourself.final fields reliably.Many teams prefer explicit construction, because it makes dependencies visible and avoids reflection:
Use @InjectMocks for convenience in classes with several dependencies; use explicit construction when you want maximum clarity. In Spring Boot tests, @MockitoBean (Spring Framework 6.2 and later, replacing the deprecated @MockBean) replaces beans in the application context, which is a different job from @InjectMocks.
No. It creates the real object under test. Mocks come from @Mock, and @InjectMocks places them into that object.
Mockito was never initialised. Add @ExtendWith(MockitoExtension.class) for JUnit 5 or call MockitoAnnotations.openMocks(this) in a setup method.
No mock of a matching type was declared, or constructor injection ran first and skipped the setter or field you expected. Check the types and names.
Yes. Initialise the field with new and Mockito injects mocks into that instance rather than creating a new one, using setter or field injection.
@InjectMocks builds the class under test and fills it with @Mock and @Spy objects, trying constructor, then setter, then field injection. Enable it with MockitoExtension, keep mock names aligned with field names when types collide, and fall back to manual construction when you want dependencies to be explicit.