### Question:
PasswordRecoveryServiceImpl implements PasswordRecoveryService { @Transactional @Override public void sendPasswordRecoveryEmailTo(String email) { User user = userRepo .findByEmail(email) .orElseThrow(() -> new NotFoundException(ErrorMessage.USER_NOT_FOUND_BY_EMAIL + email)); RestorePasswordEmail restorePasswordEmail = user.getRestorePasswordEmail(); if (restorePasswordEmail != null) { throw new WrongEmailException(PASSWORD_RESTORE_LINK_ALREADY_SENT + email); } savePasswordRestorationTokenForUser(user, jwtTool.generateTokenKey()); } PasswordRecoveryServiceImpl(
        RestorePasswordEmailRepo restorePasswordEmailRepo,
        UserRepo userRepo,
        ApplicationEventPublisher applicationEventPublisher,
        RabbitTemplate rabbitTemplate,
        JwtTool jwtTool
    ); @Transactional @Override void sendPasswordRecoveryEmailTo(String email); @Transactional @Override void updatePasswordUsingToken(String token, String newPassword);  }### Answer:
@Test(expected = NotFoundException.class) public void sendPasswordRecoveryEmailToNonExistentUserTest() { String email = "foo"; when(userRepo.findByEmail(email)).thenReturn(Optional.empty()); passwordRecoveryService.sendPasswordRecoveryEmailTo(email); }
@Test(expected = WrongEmailException.class) public void sendPasswordRecoveryEmailToUserWithExistentRestorePasswordEmailTest() { String email = "foo"; when(userRepo.findByEmail(email)).thenReturn(Optional.of( User.builder().restorePasswordEmail(new RestorePasswordEmail()).build() )); passwordRecoveryService.sendPasswordRecoveryEmailTo(email); }
@Test public void sendPasswordRecoveryEmailToSimpleTest() { String email = "foo"; User user = new User(); when(userRepo.findByEmail(email)).thenReturn(Optional.of(user)); String token = "bar"; when(jwtTool.generateTokenKey()).thenReturn(token); ReflectionTestUtils.setField(passwordRecoveryService, "tokenExpirationTimeInHours", 24); passwordRecoveryService.sendPasswordRecoveryEmailTo(email); verify(restorePasswordEmailRepo).save(refEq( RestorePasswordEmail.builder() .user(user) .token(token) .build(), "expiryDate" )); verify(rabbitTemplate).convertAndSend( refEq(sendEmailTopic), refEq(PASSWORD_RECOVERY_ROUTING_KEY), refEq(new PasswordRecoveryMessage( user.getId(), user.getName(), user.getEmail(), token )) ); }