The @InjectMocks annotation tells Mockito to create an instance of the class under test and inject the mocks declared with @Mock or @Spy into it. It saves you from building the object by hand in every test, but it fails quietly when injection does not work, so you need to know the rules it follows.
Combine it with @ExtendWith(MockitoExtension.class) in JUnit 5, which creates the mocks and runs the injection before each test.
Assume a service that depends on a repository, passed through the constructor.
The test declares the mock and the target, and Mockito wires them together.
MockitoExtension replaces the older call to MockitoAnnotations.openMocks(this) and also closes the mocks after each test. It additionally enables strict stubs, so an unused when(...) stubbing fails the test with UnnecessaryStubbingException. This catches dead setup code.
Mockito tries three strategies in a fixed order and stops at the first that succeeds.
null for it.Constructor injection is used only when the class has a constructor with arguments. When it works, Mockito does not try setter or field injection afterwards. That is why a class with both a constructor and a field that is not set by the constructor can be left with a null field.
Because Mockito does not report an injection that did not happen, a failed injection usually appears later as a NullPointerException in the code under test. The common causes are:
@Mock in the test, so it receives null.final field in a class that is created with a no-argument constructor. Field injection skips final and static fields.When the dependency graph is more than a couple of objects, write the construction yourself.
An explicit constructor call is checked by the compiler, so a missing dependency fails at build time instead of at runtime.
@Spy wraps a real object so some methods run for real and others are stubbed, and @InjectMocks injects spies as well as mocks. You can also initialise the object yourself when it needs particular arguments.
Mockito then uses the existing instance and injects mocks into it using setters or fields. Be aware that this skips constructor injection.
Mockito 5 uses the inline mock maker by default and requires Java 11 or later. You need the mockito-junit-jupiter artifact for MockitoExtension. If you use JUnit 4, annotate the class with @RunWith(MockitoJUnitRunner.class) or add a MockitoRule.
Yes. It instantiates the class under test for real and injects mocks into it. Only the dependencies are mocks.
You can in plain unit tests that do not start the Spring context. If you want Spring to create the bean, use @MockitoBean (Spring Framework 6.2 and later) or the older @MockBean with @SpringBootTest instead.
Mockito injects on a best-effort basis and does not validate the result. Check for null fields when you see a NullPointerException, or build the object manually.
For classes with one or two dependencies it is convenient. For larger classes, constructing the object explicitly is safer and clearer.
@InjectMocks creates the class under test and injects @Mock and @Spy objects using constructor, setter or field injection, in that order. Use MockitoExtension to run it with JUnit 5. Remember that injection failures are silent, so when a dependency is missing, construct the object yourself.