### Question:
DefaultUserAccessor implements UserAccessor { @Override public void deleteUser(String userName) throws AlertForbiddenOperationException { Optional<UserEntity> optionalUser = userRepository.findByUserName(userName); if (optionalUser.isPresent()) { UserEntity user = optionalUser.get(); deleteUserEntity(user); } } @Autowired  DefaultUserAccessor(UserRepository userRepository, UserRoleRepository userRoleRepository, PasswordEncoder defaultPasswordEncoder, DefaultRoleAccessor roleAccessor,
        AuthenticationTypeAccessor authenticationTypeAccessor); @Override List<UserModel> getUsers(); @Override Optional<UserModel> getUser(Long userId); @Override Optional<UserModel> getUser(String username); @Override UserModel addUser(String userName, String password, String emailAddress); @Override UserModel addUser(UserModel user, boolean passwordEncoded); @Override UserModel updateUser(UserModel user, boolean passwordEncoded); @Override boolean assignRoles(String username, Set<Long> roleIds); @Override boolean changeUserPassword(String username, String newPassword); @Override boolean changeUserEmailAddress(String username, String emailAddress); @Override void deleteUser(String userName); @Override void deleteUser(Long userId);  }### Answer:
@Test public void deleteUserByNameTest() throws Exception { UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L); userEntity.setId(5L); Mockito.when(userRepository.findByUserName(Mockito.eq(username))).thenReturn(Optional.of(userEntity)); DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor); defaultUserAccessor.deleteUser(username); Mockito.verify(roleAccessor).updateUserRoles(Mockito.any(), Mockito.any()); Mockito.verify(userRepository).deleteById(Mockito.any()); }
@Test public void deleteUserByIdTest() throws Exception { UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L); userEntity.setId(5L); Mockito.when(userRepository.findById(Mockito.eq(userEntity.getId()))).thenReturn(Optional.of(userEntity)); DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor); defaultUserAccessor.deleteUser(userEntity.getId()); Mockito.verify(roleAccessor).updateUserRoles(Mockito.any(), Mockito.any()); Mockito.verify(userRepository).deleteById(Mockito.any()); }
@Test public void deleteUserReservedIdTest() throws Exception { UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L); userEntity.setId(1L); Mockito.when(userRepository.findByUserName(Mockito.eq(username))).thenReturn(Optional.of(userEntity)); Mockito.when(userRepository.findById(Mockito.any())).thenReturn(Optional.of(userEntity)); DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor); try { defaultUserAccessor.deleteUser(username); fail("A forbidden userEntity id did not throw the expected AlertForbiddenOperationException"); } catch (AlertForbiddenOperationException e) { assertNotNull(e); } }