UserService { @Scheduled(cron = "0 0 0 * * ?") public void removeOldPersistentTokens() { LocalDate now = new LocalDate(); persistentTokenRepository.findByTokenDateBefore(now.minusMonths(1)).stream().forEach(token ->{ log.debug("Deleting token {}", token.getSeries()); User user = token.getUser(); user.getPersistentTokens().remove(token); persistentTokenRepository.delete(token); }); }  Optional<User> activateRegistration(String key); Optional<User> completePasswordReset(String newPassword, String key); Optional<User> requestPasswordReset(String mail); User createUserInformation(String login, String password, String firstName, String lastName, String email,
                                      String langKey); void updateUserInformation(String firstName, String lastName, String email, String langKey); void changePassword(String password); @Transactional(readOnly = true) User getUserWithAuthorities(); @Scheduled(cron = "0 0 0 * * ?") void removeOldPersistentTokens(); @Scheduled(cron = "0 0 1 * * ?") void removeNotActivatedUsers();  }
@Test public void testRemoveOldPersistentTokens() { User admin = userRepository.findOneByLogin("admin").get(); int existingCount = persistentTokenRepository.findByUser(admin).size(); generateUserToken(admin, "1111-1111", new LocalDate()); LocalDate now = new LocalDate(); generateUserToken(admin, "2222-2222", now.minusDays(32)); assertThat(persistentTokenRepository.findByUser(admin)).hasSize(existingCount + 2); userService.removeOldPersistentTokens(); assertThat(persistentTokenRepository.findByUser(admin)).hasSize(existingCount + 1); }