### Question:
PasswordRecoveryServiceImpl implements PasswordRecoveryService { @Transactional @Override public void updatePasswordUsingToken(String token, String newPassword) { RestorePasswordEmail restorePasswordEmail = restorePasswordEmailRepo .findByToken(token) .orElseThrow(() -> new BadVerifyEmailTokenException(NO_ANY_EMAIL_TO_VERIFY_BY_THIS_TOKEN)); if (isNotExpired(restorePasswordEmail.getExpiryDate())) { applicationEventPublisher.publishEvent( new UpdatePasswordEvent(this, newPassword, restorePasswordEmail.getUser().getId()) ); restorePasswordEmailRepo.delete(restorePasswordEmail); log.info("User with email " + restorePasswordEmail.getUser().getEmail() + " has successfully restored the password using token " + token); } else { log.info("Password restoration token of user with email " + restorePasswordEmail.getUser().getEmail() + " has been expired. Token: " + token); throw new UserActivationEmailTokenExpiredException(EMAIL_TOKEN_EXPIRED); } } 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 = BadVerifyEmailTokenException.class) public void updatePasswordUsingTokenWithNonExistentTokenTest() { String token = "foo"; String newPassword = "bar"; when(restorePasswordEmailRepo.findByToken(token)).thenReturn(Optional.empty()); passwordRecoveryService.updatePasswordUsingToken(token, newPassword); }
@Test(expected = UserActivationEmailTokenExpiredException.class) public void updatePasswordUsingTokenWithExpiredTokenTest() { String token = "foo"; String newPassword = "bar"; when(restorePasswordEmailRepo.findByToken(token)).thenReturn(Optional.of( RestorePasswordEmail.builder() .expiryDate(LocalDateTime.now().minusHours(1)) .user(User.builder().email("foo@bar.com").build()) .build() )); passwordRecoveryService.updatePasswordUsingToken(token, newPassword); }
@Test public void updatePasswordUsingTokenSimpleTest() { String token = "foo"; String newPassword = "bar"; User user = User.builder().id(1L).email("foo@bar.com").build(); when(restorePasswordEmailRepo.findByToken(token)).thenReturn(Optional.of( RestorePasswordEmail.builder() .expiryDate(LocalDateTime.now().plusHours(1)) .user(user) .token(token) .build() )); passwordRecoveryService.updatePasswordUsingToken(token, newPassword); verify(restorePasswordEmailRepo).delete(refEq( RestorePasswordEmail.builder() .user(user) .token(token) .build(), "expiryDate" )); verify(applicationEventPublisher).publishEvent(refEq( new UpdatePasswordEvent(passwordRecoveryService.getClass(), newPassword, user.getId()), "timestamp" )); }