### Question:
DefaultJwtService implements JwtService { @Override public Optional<String> getSubFromToken(String token) { try { Jws<Claims> claimsJws = Jwts.parser().setSigningKey(secret).parseClaimsJws(token); return Optional.ofNullable(claimsJws.getBody().getSubject()); } catch (Exception e) { return Optional.empty(); } } @Autowired  DefaultJwtService(@Value("${jwt.secret}") String secret,
                             @Value("${jwt.sessionTime}") int sessionTime); @Override String toToken(User user); @Override Optional<String> getSubFromToken(String token);  }### Answer:
@Test public void should_get_null_with_wrong_jwt() throws Exception { Optional<String> optional = jwtService.getSubFromToken("123"); assertThat(optional.isPresent(), is(false)); }
@Test public void should_get_null_with_expired_jwt() throws Exception { String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhaXNlbnNpeSIsImV4cCI6MTUwMjE2MTIwNH0.SJB-U60WzxLYNomqLo4G3v3LzFxJKuVrIud8D8Lz3-mgpo9pN1i7C8ikU_jQPJGm8HsC1CquGMI-rSuM7j6LDA"; assertThat(jwtService.getSubFromToken(token).isPresent(), is(false)); }