DefaultUserService implements UserService { @Override public User createUser(String username, String email, String password) { if (repository.findByUsernameIgnoreCase(username) != null || repository.findByEmailIgnoreCase(email) != null) { throw new IllegalArgumentException(); } final User user = new User(); user.setUsername(username); user.setEmail(email); user.setPasswordHash(EncryptionUtils.encrypt(password)); return repository.save(user); } DefaultUserService(UserRepository repository, GroupService groupService); @Override User createUser(String username, String email, String password); @Override void updatePassword(String handle, String oldPassword, String newPassword); @Override void deleteUser(String handle); @Override User lookup(String handle); @Override User authenticate(String handle, String password);  }
@Test public void testCreatingAUser() { assertThat(repository.count(), is(0L)); final String username = "milad"; final String email = "milad@mmnaseri.com"; final String password = "123456"; final User user = service.createUser(username, email, password); assertThat(user, is(notNullValue())); assertThat(user.getId(), is(notNullValue())); assertThat(user.getUsername(), is(username)); assertThat(user.getEmail(), is(email)); assertThat(user.getPasswordHash(), is(not(password))); assertThat(repository.count(), is(1L)); final User found = repository.findById(user.getId()).orElse(null); assertThat(found, is(notNullValue())); assertThat(found.getUsername(), is(username)); assertThat(found.getEmail(), is(email)); assertThat(found.getPasswordHash(), is(user.getPasswordHash())); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testCreatingADuplicateUser() { service.createUser("milad", "email1", "123456"); service.createUser("milad", "email2", "123456"); }