### Question:
UserManagementServiceImpl implements UserManagementService { @Override @Transactional public boolean deactivateUser(UserDetail userDetail) { User user = userDao.getUserForId(userDetail.getId()); if(user == null) { log.info("User '{}' does not exist in the system", userDetail.getUserName()); return false; } for(Iterator<UserIndex> it = user.getUserIndices().iterator(); it.hasNext();) { UserIndex userIndex = it.next(); userDao.deleteUserIndex(userIndex); it.remove(); } userDao.saveOrUpdateUser(user); log.info("User '{}' deactivated 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 testDeactivateNonExistingUser() { UserDetail userDetail = mock(UserDetail.class); when(userDetail.getId()).thenReturn(1); when(userDetail.getUserName()).thenReturn("admin2"); when(userDao.getUserForId(1)).thenReturn(null); boolean success = service.deactivateUser(userDetail); assertFalse("Unable to deactivate non existing user", success); verify(userDao, times(0)).deleteUserIndex(isA(UserIndex.class)); verify(userDao, times(0)).saveOrUpdateUser(user); }
@Test public void testDeactivateUser() { Integer userId = 1; UserDetail userDetail = mock(UserDetail.class); buildUserDetail(userId, userDetail, "password"); UserIndex userIndex = mock(UserIndex.class); Set<UserIndex> userIndices = new HashSet<UserIndex>(); userIndices.add(userIndex); when(userDao.getUserForId(userId)).thenReturn(user); when(user.getUserIndices()).thenReturn(userIndices); boolean success = service.deactivateUser(userDetail); assertTrue("User deactivated successfully", success); verify(userDao).deleteUserIndex(isA(UserIndex.class)); verify(userDao).saveOrUpdateUser(user); }