AuthenticationServiceImpl implements AuthenticationService { @Override public String authenticate(String username, String password) { User user = userRepository.findByUsernameAndPassword(username, password); if (user == null) { throw new UnauthorizedAccessException("No user matches username " + username + " and password"); } return tokenStore.generate(username); } AuthenticationServiceImpl(
      TokenStore tokenStore,
      UserRepository userRepository); @Override String authenticate(String username, String password); @Override String validate(String token);  }
@Test public void authenticateUserWithUsernameAndPassword() { when(userRepository.findByUsernameAndPassword(username, password)).thenReturn(user); when(tokenStore.generate(username)).thenReturn(token); String token = authenticationService.authenticate(username, password); assertThat(token).isEqualTo(this.token); }
@Test public void blowsUpWhenUserIsInvalid() { try { authenticationService.authenticate(username, password); expectFailing(UnauthorizedAccessException.class); } catch (UnauthorizedAccessException e) { assertThat(e.getMessage()).isEqualTo("No user matches username " + username + " and password"); } }