Currently, I am having some trouble with mockito library... more exactly I am unable to inject the userRepository bean into my mocked service. For dependency injection, I am using Spring Boot. Below are some code samples:
@Service
public class UserService {
@Autowired private TokenService tokenService;
@Autowired private UserRepository userRepository;
public User updateUser(User user) {
validateUser(user); // can throw some validation errors
createToken(user); // creates token to my user
return userRepository.save(user);
}
}
In my test method, I want to skip the validation and the token creation before the user gets updated.
UserService userService = mock(UserService.class);
doNothing(userService).validateUser(any());
doNothing(userService).createToken(any());
So the problem occurs in my test method when I call the updateUser method, it fails because my userRepository is not injected (NullPointerException). I tried with @Mock and @InjectMocks, but these annotations are used to inject mocks. How can I inject a real bean into my mock? Is this possible using mockito? Thank you.
doNothing(userService).validateUser(any());compile? In mockito version 1.9.5 and 2.0.2 you only have aStubber Mockito::doNothing()method - Daniele