### Question:
JwtGenerator { String createJwt() throws JoseException { Instant now = mClock.instant(); mClaims.setIssuedAt(NumericDate.fromMilliseconds(now.toEpochMilli())); mClaims.setExpirationTime( NumericDate.fromMilliseconds(now.plus(mTokenLifetime).toEpochMilli())); mJws.setPayload(mClaims.toJson()); return mJws.getCompactSerialization(); } JwtGenerator(
            @NonNull KeyPair keyPair,
            @NonNull String jwtAudience,
            @NonNull Duration tokenLifetime); @VisibleForTesting()  JwtGenerator(
            @NonNull KeyPair keyPair,
            @NonNull String jwtAudience,
            @NonNull Duration tokenLifetime,
            @NonNull Clock clock);   }### Answer:
@Test public void testCreateJwtRsa() throws JoseException { JwtGenerator jwtGenerator = new JwtGenerator(RSA_KEY_PAIR, JWT_AUDIENCE, TOKEN_LIFETIME, TEST_CLOCK); String rawJwt = jwtGenerator.createJwt(); Jws<Claims> parsedJwt = Jwts.parser() .setSigningKey(RSA_KEY_PAIR.getPublic()) .parseClaimsJws(rawJwt); JwsHeader header = parsedJwt.getHeader(); Claims claims = parsedJwt.getBody(); assertThat(header.getAlgorithm()).isEqualTo("RS256"); assertThat(header.getType()).isEqualTo("JWT"); assertThat(claims.getAudience()).isEqualTo(JWT_AUDIENCE); assertThat(claims.getIssuedAt().getTime()).isAtLeast(TEST_CLOCK.millis() - 1000); assertThat(claims.getIssuedAt().getTime()).isAtMost(TEST_CLOCK.millis() + 1000); assertThat(claims.getExpiration().getTime()) .isLessThan(Clock.offset(TEST_CLOCK, TOKEN_LIFETIME.plusSeconds(1)).millis()); assertThat(claims.getExpiration().getTime()) .isAtLeast(Clock.offset(TEST_CLOCK, TOKEN_LIFETIME.minusSeconds(1)).millis()); }
@Test public void testCreateJwtEc() throws JoseException { JwtGenerator jwtGenerator = new JwtGenerator(EC_KEY_PAIR, JWT_AUDIENCE, TOKEN_LIFETIME, TEST_CLOCK); String rawJwt = jwtGenerator.createJwt(); Jws<Claims> parsedJwt; try { parsedJwt = Jwts.parser() .setSigningKey(EC_KEY_PAIR.getPublic()) .parseClaimsJws(rawJwt); } catch (UnsupportedJwtException | MalformedJwtException | SignatureException e) { fail("Error parsing JWT: " + e); return; } JwsHeader header = parsedJwt.getHeader(); Claims claims = parsedJwt.getBody(); assertThat(header.getAlgorithm()).isEqualTo("ES256"); assertThat(header.getType()).isEqualTo("JWT"); assertThat(claims.getAudience()).isEqualTo(JWT_AUDIENCE); assertThat(claims.getIssuedAt().getTime()).isAtLeast(TEST_CLOCK.millis() - 1000); assertThat(claims.getIssuedAt().getTime()).isAtMost(TEST_CLOCK.millis() + 1000); assertThat(claims.getExpiration().getTime()) .isLessThan(Clock.offset(TEST_CLOCK, TOKEN_LIFETIME.plusSeconds(1)).millis()); assertThat(claims.getExpiration().getTime()) .isAtLeast(Clock.offset(TEST_CLOCK, TOKEN_LIFETIME.minusSeconds(1)).millis()); }