TokenAuthentication implements Authentication { @Nullable @Override public String getUserId() { if (tokenInfo == null) { throw new IllegalStateException("UserId can only be retrieved after successful authentication."); } return tokenInfo.getUserId(); } TokenAuthentication(@NotNull TokenProvider tokenProvider); @Override boolean authenticate(@Nullable Credentials credentials); @Nullable @Override String getUserId(); @Nullable @Override Principal getUserPrincipal();  }
@Test(expected = IllegalStateException.class) public void testGetUserIdBeforeLogin() { authentication.getUserId(); }
@Test public void testGetUserId() throws LoginException { TokenInfo info = createTokenInfo(tokenProvider, userId); assertTrue(authentication.authenticate(new TokenCredentials(info.getToken()))); assertEquals(userId, authentication.getUserId()); }