A very simple example is that if you have a UserService
that has @Autowired
jpa resposiroty UserRepository
...
class UserService{
@Autowired
UserRepository userRepository;
...
}
then in the test class for UserService
you will do
...
class TestUserService{
@Mock
UserRepository userRepository;
@InjectMocks
UserService userService;
...
}
@InjectMocks
tells the framework that take the @Mock UserRepository userRespository;
and inject that into userService
so rather than auto wiring a real instance of UserRepository
a Mock of UserRepository
will be injected in userService
.