### Question:
DefaultUserAccessor implements UserAccessor { @Override public UserModel addUser(String userName, String password, String emailAddress) throws AlertDatabaseConstraintException { return addUser(UserModel.newUser(userName, password, emailAddress, AuthenticationType.DATABASE, Collections.emptySet(), true), false); } @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 addUserTest() throws Exception { final String roleName = "userName"; UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L); userEntity.setId(1L); AuthenticationTypeDetails authenticationTypeDetails = new AuthenticationTypeDetails(2L, "authentication-name"); UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L); UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true); Mockito.when(userRepository.findByUserName(Mockito.any())).thenReturn(Optional.empty()); Mockito.when(authenticationTypeAccessor.getAuthenticationTypeDetails(Mockito.any())).thenReturn(Optional.of(authenticationTypeDetails)); Mockito.when(userRepository.save(Mockito.any())).thenReturn(userEntity); createModelMocks(userRoleRelation, userRoleModel, AuthenticationType.DATABASE); DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor); UserModel userModel = defaultUserAccessor.addUser(username, password, emailAddress); testUserModel(userEntity.getId(), username, emailAddress, roleName, userModel); }
@Test public void addUserExistsTest() throws Exception { UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L); userEntity.setId(1L); Mockito.when(userRepository.findByUserName(Mockito.any())).thenReturn(Optional.of(userEntity)); DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor); try { defaultUserAccessor.addUser(username, password, emailAddress); fail("User with the same name that already exists in the userRepository did not throw expected AlertForbiddenOperationException."); } catch (AlertDatabaseConstraintException e) { assertNotNull(e); } }