### Question:
UserManagementServiceImpl implements UserManagementService { @Override @Transactional public boolean updateUser(UserDetail userDetail) { User user = userDao.getUserForId(userDetail.getId()); if(user == null) { log.info("User '{}' does not exist in the system", userDetail.getUserName()); return false; } String salt = _hasCryptoEncoder ? null : userDetail.getUserName(); if(StringUtils.isNotBlank(userDetail.getPassword())) { String credentials = passwordEncoder.encodePassword(userDetail.getPassword(), salt); for(UserIndex userIndex : user.getUserIndices()) { userIndex.setCredentials(credentials); } } updateRole(userDetail.getRole(), user); userDao.saveOrUpdateUser(user); log.info("User '{}' updated successfully", userDetail.getUserName()); return true; }  @Override @Transactional(readOnly=true) List<String> getUserNames(final String searchString); @Override @Transactional(readOnly=true) UserDetail getUserDetail(final String userName); @Override @Transactional void disableOperatorRole(User user); @Override @Transactional boolean createUser(String userName, String password, boolean admin); @Override @Transactional boolean updateUser(UserDetail userDetail); @Override @Transactional boolean deactivateUser(UserDetail userDetail); @Autowired void setSesssionFactory(SessionFactory sessionFactory); @Autowired void setAuthoritiesService(StandardAuthoritiesService authoritiesService); @Autowired void setUserDao(UserDao userDao); @Autowired void setUserService(UserService userService); @Autowired void setPasswordEncoder(PasswordEncoder passwordEncoder); void setPasswordEncoder(Object passwordEncoder);  }### Answer:
@Test public void testUpdateNonExistingUser() { UserDetail userDetail = mock(UserDetail.class); when(userDetail.getId()).thenReturn(1); when(userDetail.getUserName()).thenReturn("admin2"); when(userDao.getUserForId(1)).thenReturn(null); boolean success = service.updateUser(userDetail); assertFalse("Unable to update non existing user", success); verify(passwordEncoder, times(0)).encodePassword("password", "admin2"); verify(userDao, times(0)).saveOrUpdateUser(user); }
@Test public void testUpdateUser() { String credentials = "encryptedPassword"; Integer userId = 1; UserDetail userDetail = mock(UserDetail.class); UserIndex userIndex = mock(UserIndex.class); UserRole userRole = mock(UserRole.class); UserRole adminRole = mock(UserRole.class); Set<UserIndex> userIndices = new HashSet<UserIndex>(); userIndices.add(userIndex); Set<UserRole> userRoles = new HashSet<UserRole>(); userRoles.add(userRole); when(userRole.getName()).thenReturn("ROLE_USER"); buildUserDetail(userId, userDetail, "password"); when(userDao.getUserForId(userId)).thenReturn(user); when(passwordEncoder.encodePassword("password", "admin")).thenReturn(credentials); when(user.getUserIndices()).thenReturn(userIndices); when(user.getRoles()).thenReturn(userRoles); when(authoritiesService.getAdministratorRole()).thenReturn(adminRole); boolean success = service.updateUser(userDetail); assertTrue("User updated successfully", success); verify(passwordEncoder).encodePassword("password", "admin"); verify(authoritiesService).getAdministratorRole(); verify(userDao).saveOrUpdateUser(user); }