@InjectMocks annotations tell Mockito to insert all mocks (objects annotated with @Mock annotation) into the fields of the test object. Mockito uses Reflection for this.
@Autowired annotation tells the Spring structure to insert a bean from its IoC container. Spring also uses reflection for this when it is a private field injection. You can even use the @Inject annotation (part of the Java EE specification) with the same effect.
But I would suggest looking at the benefits of Injection Constructor over Field Injection . In this case, you donβt need to use @InjectMocks , because you can pass mocks to the test object through the constructor. In your test and in production there would be no reflection needed under the hood.
If you want to create an integration test with a subset of Spring beans, I would suggest looking at the @DirtiesContext annotation. It is part of the Spring frame module, commonly called the "Spring Test".
source share