AuthenticationServiceImpl implements AuthenticationService { @Override public String validate(String token) { try { return tokenStore.parse(token); } catch (TokenException e) { throw new UnauthorizedAccessException("No user matches such a token " + token, e); } } AuthenticationServiceImpl(
      TokenStore tokenStore,
      UserRepository userRepository); @Override String authenticate(String username, String password); @Override String validate(String token);  }
@Test public void blowsUpWhenTokenMatchesNoUser() { when(tokenStore.parse(token)).thenThrow(new TokenException()); try { authenticationService.validate(token); expectFailing(UnauthorizedAccessException.class); } catch (UnauthorizedAccessException e) { assertThat(e.getMessage()).isEqualTo("No user matches such a token " + token); } }
@Test public void validatesUserToken() { when(tokenStore.parse(token)).thenReturn(username); String user = authenticationService.validate(token); assertThat(user).isEqualTo(username); }