UserServiceImpl implements UserService { @Transactional public User persistEditedUser(User user) throws ObjectNotUniqueException { User dbUser = userDAO.findByUsername(user.getUsername()); if (dbUser != null && !dbUser.getUserId().equals(user.getUserId())) { throw new ObjectNotUniqueException("Username already in use"); } else if (dbUser == null) { dbUser = findUserOnId(user); } dbUser.setActive(user.isActive()); dbUser.setEmail(user.getEmail()); dbUser.setFirstName(user.getFirstName()); dbUser.setLastName(user.getLastName()); dbUser.getUserDepartments().clear(); dbUser.getUserDepartments().addAll(user.getUserDepartments()); dbUser.setUsername(user.getUsername()); boolean reAddPm = dbUser.getUserRoles().contains(UserRole.PROJECTMANAGER); dbUser.setUserRoles(user.getUserRoles()); if (reAddPm && !user.getUserRoles().contains(UserRole.PROJECTMANAGER)) { dbUser.addUserRole(UserRole.PROJECTMANAGER); } userDAO.persist(dbUser); return dbUser; }  @Transactional(readOnly = true) User getUser(Integer userId); @Transactional(readOnly = true) User getUserAndCheckDeletability(Integer userId); @Transactional(readOnly = true) User getUser(String username); @Transactional(readOnly = true) List<UserDepartment> getUserDepartments(); @Transactional UserDepartment persistUserDepartment(UserDepartment department); @Transactional(readOnly = true) UserDepartment getUserDepartment(Integer departmentId); @Transactional(readOnly = true) List<User> getUsers(); @Transactional(readOnly = true) List<User> getActiveUsers(); @Transactional(readOnly = true) List<UserRole> getUserRoles(); @Transactional User persistEditedUser(User user); @Override @Transactional void persistNewUser(User user, String password); @Override @Transactional void changePassword(String username, String currentPassword, String newUnencryptedPassword); @Override @Transactional void changePassword(String username, String newUnencryptedPassword); List<User> getUsersWithEmailSet(); @Override @Transactional User validateProjectManagementRoles(Integer userId); @Override @Transactional(readOnly = true) List<User> getUsers(UserRole userRole); @Transactional void deleteUser(Integer userId); @Transactional void deleteDepartment(Integer departmentId); void setPasswordEncoder(MessageDigestPasswordEncoder passwordEncoder); void setProjectAssignmentManagementService(ProjectAssignmentManagementService projectAssignmentManagementService); void setUserDAO(UserDao dao); void setUserDepartmentDAO(UserDepartmentDao dao); void setUserRoleDAO(UserRoleDao dao);  }
@Test public void shouldChangeUsername() throws ObjectNotUniqueException { User user = UserObjectMother.createUser(); User persistedUser = new User(); when(userDAO.findByUsername(user.getUsername())).thenReturn(null); when(userDAO.findById(user.getUserId())).thenReturn(persistedUser); when(userDAO.persist(persistedUser)).thenReturn(persistedUser); userService.persistEditedUser(user); verify(userDAO).persist(persistedUser); }