UserServiceImpl implements UserService { @Override @Transactional public void changePassword(String username, String currentPassword, String newUnencryptedPassword) throws BadCredentialsException { User user = userDAO.findByUsername(username); Validate.notNull(user, String.format("Can't find user with username %s", username)); String encryptedCurrentPassword = encryptPassword(currentPassword, user.getSalt()); if (!user.getPassword().equals(encryptedCurrentPassword)) { throw new BadCredentialsException("Invalid current password"); } changePassword(user, newUnencryptedPassword); }  @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 shouldUpdatePassword() throws ObjectNotUniqueException { User user = new User(1); user.setPassword("aa"); user.setUsername("user"); when(userDAO.findByUsername("user")).thenReturn(user); when(userDAO.persist(any(User.class))).thenReturn(user); userService.changePassword("user", "pwd"); assertFalse(user.getPassword().equals("pwd")); }