### Question:
DefaultUserAccessor implements UserAccessor { @Override public Optional<UserModel> getUser(Long userId) { return userRepository.findById(userId).map(this::createModel); } @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 getUserByUserIdTest() { final Long userId = 1L; final Long emptyUserId = 5L; final Long authenticationTypeId = 1L; final String roleName = "userName"; UserEntity userEntity = new UserEntity(username, password, emailAddress, authenticationTypeId); userEntity.setId(1L); UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L); UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true); Mockito.when(userRepository.findById(userId)).thenReturn(Optional.of(userEntity)); Mockito.when(userRepository.findById(emptyUserId)).thenReturn(Optional.empty()); createModelMocks(userRoleRelation, userRoleModel, AuthenticationType.DATABASE); DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor); Optional<UserModel> userModelOptional = defaultUserAccessor.getUser(userId); Optional<UserModel> userModelOptionalEmpty = defaultUserAccessor.getUser(emptyUserId); assertTrue(userModelOptional.isPresent()); assertFalse(userModelOptionalEmpty.isPresent()); UserModel userModel = userModelOptional.get(); testUserModel(userEntity.getId(), username, emailAddress, roleName, userModel); }
@Test public void getUserByUsernameTest() { final String emptyUsername = ""; final Long authenticationTypeId = 1L; final String roleName = "userName"; UserEntity userEntity = new UserEntity(username, password, emailAddress, authenticationTypeId); userEntity.setId(1L); UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L); UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true); Mockito.when(userRepository.findByUserName(username)).thenReturn(Optional.of(userEntity)); Mockito.when(userRepository.findByUserName(emptyUsername)).thenReturn(Optional.empty()); createModelMocks(userRoleRelation, userRoleModel, AuthenticationType.DATABASE); DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor); Optional<UserModel> userModelOptional = defaultUserAccessor.getUser(username); Optional<UserModel> userModelOptionalEmpty = defaultUserAccessor.getUser(emptyUsername); assertTrue(userModelOptional.isPresent()); assertFalse(userModelOptionalEmpty.isPresent()); UserModel userModel = userModelOptional.get(); testUserModel(userEntity.getId(), username, emailAddress, roleName, userModel); }