UserService { public User register(UserRegister user) { User newUser = new User(); newUser.setLogin(user.getLogin()); newUser.setEmail(user.getEmail()); newUser.setFirstName(user.getFirstName()); newUser.setLastName(user.getLastName()); newUser.setPassword(this.passwordEncoder.encode(user.getPassword())); newUser.setLangKey(user.getLangKey()); newUser.setRegisterDate(ZonedDateTime.now()); newUser.setActivationKey(RandomStringUtils.randomNumeric(KEY_SIZE)); this.authorityRepository.findById(Constants.USER_AUTHORITY) .ifPresent(authority -> newUser.getAuthorities().add(authority)); return this.userRepository.save(newUser); } 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 whenCreateUserThenGetIt() { UserRegister user = new UserRegister(); user.setLogin("testlogin"); user.setEmail("test@email.com"); user.setFirstName("First"); user.setLastName("Last"); user.setPassword("password"); user.setLangKey("en"); this.userService.register(user); User result = this.userRepository.findOneWithAuthoritiesByLogin("testlogin").get(); assertEquals(result.getLogin(), user.getLogin()); assertEquals(result.getLogin(), user.getLogin()); assertTrue(result.getAuthorities().contains(this.authorityRepository.findById(Constants.USER_AUTHORITY).get())); }