I have the service class:
@Service
@RequiredArgsConstructor
public class UserService {
private final UserRepository userRepository;
@Transactional
public void registerUser(User user) {
user.setPassword(DigestUtils.md5Hex(user.getPassword()));
userRepository.save(user);
}
}
And I have the following test:
@RunWith(MockitoJUnitRunner.class)
public class UserServiceTests {
private UserRepository userRepository = Mockito.mock(UserRepository.class);
private UserService userService = new UserService(userRepository);
@Test(expected = Exception.class)
public void testCreateUser(){
User user = new User(null, "Glass", "123123", "glass999@mail.ru");
when(userRepository.save(null)).thenThrow(new Exception());
userService.registerUser(user);
}
}
And my question is why the test passes?? It must passes only when userRepository's save method accepts null. But I pass user object, not null, and save method actually accepts user object. Does anyone know the answer?