### Question:
BasicUserValidator implements UserValidator { @Override public Optional<User> getUserFromAccessToken(String accessToken) throws UserValidationException { if (accessToken != null) { try { Optional<User> user = loggedInUsers.userFor(accessToken); return user; } catch (Exception e) { throw new UserValidationException(e); } } return Optional.empty(); } BasicUserValidator(UserStore userStore, LoggedInUsers loggedInUsers); @Override Optional<User> getUserFromAccessToken(String accessToken); @Override Optional<User> getUserFromUserId(String userId); @Override Optional<User> getUserFromPersistentId(String persistentId);  }### Answer:
@Test public void getUserFromAccessTokenReturnsEmptyWhenAccessTokenIsNull() throws Exception { Optional<User> user = basicUserValidator.getUserFromAccessToken(null); assertThat(user, is(Optional.empty())); }
@Test public void getUserFromAccessTokenReturnsUserWhenAccessTokenIsValid() throws Exception { given(loggedInUsers.userFor("validAccessToken")).willReturn(Optional.of(anyUser())); BasicUserValidator basicUserValidator2 = new BasicUserValidator(userStore, loggedInUsers); Optional<User> user = basicUserValidator2.getUserFromAccessToken("validAccessToken"); assertThat(user, is(OptionalPresentMatcher.present())); }
@Test public void getUserFromAccessTokenReturnsEmptyWhenAccessTokenIsInvalid() throws Exception { given(loggedInUsers.userFor("invalidAccessToken")).willReturn( null ); BasicUserValidator basicUserValidator2 = new BasicUserValidator(userStore, loggedInUsers); Optional<User> user = basicUserValidator2.getUserFromAccessToken("validAccessToken"); assertThat(user, is(Optional.empty())); }