### Question:
UserService extends
    PersonService<E, D> { public void updatePassword(E user, String rawPassword) throws Exception { if (user.getId() == null) { throw new Exception("The ID of the user object is null."); } user.setPassword(passwordEncoder.encode(rawPassword)); dao.saveOrUpdate(user); } @SuppressWarnings("unchecked")  UserService(); protected  UserService(Class<E> entityClass); @Override @Autowired @Qualifier("userDao") void setDao(D dao); @PostAuthorize("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(#accountName, 'READ')") @Transactional(readOnly = true) E findByAccountName(String accountName); @PostAuthorize("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(#email, 'READ')") @Transactional(readOnly = true) E findByEmail(String email); E registerUser(E user, HttpServletRequest request); @SuppressWarnings("unchecked") void activateUser(String tokenValue); E persistNewUser(E user, boolean encryptPassword); void updatePassword(E user, String rawPassword); @Transactional(readOnly = true) E getUserBySession(); @PostFilter("hasRole(@configHolder.getSuperAdminRoleName()) or hasPermission(filterObject, 'READ')") @Transactional(readOnly = true) Set<UserGroup> getGroupsOfUser(Integer userId); PasswordEncoder getPasswordEncoder(); void setPasswordEncoder(PasswordEncoder passwordEncoder); Role getDefaultUserRole(); void setDefaultUserRole(Role defaultUserRole);  }### Answer:
@Test public void updatePassword_shouldUpdatePasswordAsExpected() throws Exception { String oldPassword = "eNcrYpt3dOldP4ssw0rd"; String newPassword = "r4Wn3Wp@sSw0rd"; Integer userId = 42; User user = new User(); user.setPassword(oldPassword); IdHelper.setIdOnPersistentObject(user, userId); doNothing().when(dao).saveOrUpdate(any(User.class)); crudService.updatePassword(user, newPassword); verify(dao, times(1)).saveOrUpdate(any(User.class)); verifyNoMoreInteractions(dao); String encryptedNewPassword = user.getPassword(); assertFalse(oldPassword.equals(encryptedNewPassword)); assertTrue(passwordEncoder.matches(newPassword, encryptedNewPassword)); }
@Test public void updatePassword_shouldThrowIfUserHasNoId() { String newPassword = "r4Wn3Wp@sSw0rd"; User user = new User(); try { crudService.updatePassword(user, newPassword); fail("Should have thrown Exception, but did not!"); } catch (Exception e) { assertEquals("The ID of the user object is null.", e.getMessage()); verifyNoMoreInteractions(dao); } }