UserService { @Scheduled(cron = "0 0 1 * * ?") public void removeNotActivatedUsers() { this.userRepository .findAllByActivatedIsFalseAndRegisterDateBefore( ZonedDateTime.now().minus(3, ChronoUnit.DAYS) ) .forEach(this.userRepository::delete); } UserService(UserRepository userRepository, AuthorityRepository authorityRepository, PasswordEncoder passwordEncoder); User register(UserRegister user); void update(UserDTO userDTO); void update(String id, String login, String firstName, String lastName, String email, String langKey); void delete(String login); Optional<User> activateRegistration(String key); Page<UserDTO> search(Pageable pageable, String query); Page<UserDTO> getAll(Pageable pageable); Optional<User> getById(String id); Optional<User> getByLogin(String login); Optional<User> getByEmail(String email); void changePassword(String id, String password); Optional<User> requestPasswordReset(String mail); Optional<User> completePasswordReset(String password, String key); @Scheduled(cron = "0 0 1 * * ?") void removeNotActivatedUsers();  }
@Test public void whenFindNotActivatedUsers() { ZonedDateTime now = ZonedDateTime.now(); User user = this.createUser(); user.setRegisterDate(now.minus(4, ChronoUnit.DAYS)); this.userRepository.save(user); List<User> users = this.userRepository.findAllByActivatedIsFalseAndRegisterDateBefore(now.minus(3, ChronoUnit.DAYS)); assertFalse(users.isEmpty()); this.userService.removeNotActivatedUsers(); users = userRepository.findAllByActivatedIsFalseAndRegisterDateBefore(now.minus(3, ChronoUnit.DAYS)); assertTrue(users.isEmpty()); }
@Test public void whenRemoveNotActivatedUsers() { User user = this.createUser(); user.setRegisterDate(ZonedDateTime.now().minus(30, ChronoUnit.DAYS)); this.userRepository.save(user); assertTrue(this.userRepository.findByLogin("testtest").isPresent()); this.userService.removeNotActivatedUsers(); assertFalse(this.userRepository.findByLogin("testtest").isPresent()); }