如何在Java中为AccessTokenVerifier和JwtVerifier编写junit测试用例

s4chpxco  于 2023-02-04  发布在  Java
关注(0)|答案(1)|浏览(183)

访问令牌验证程序

如何写访问令牌验证juint测试用例。我已经附上了截图,代码和依赖项的细节。请帮助,如果有人知道这一点。
请查看屏幕截图。Jwt token verifier source code screenshot

源代码

log.info("JwtVerifier start building to verify the authToken");
            AccessTokenVerifier jwtVerifier = 
                    JwtVerifiers.accessTokenVerifierBuilder()
                    .setIssuer(oktaTokenUrlIssuer)
                    .setAudience(TOKEN_AUDIENCE)
                    .setConnectionTimeout(Duration.ofSeconds(1))
                    .build();
            
            jwtVerifier.decode(authToken);
            log.info("JwtVerifier build and verified the authToken successfully");

依赖项

<dependency>
   <groupId>com.okta.jwt</groupId>
   <artifactId>okta-jwt-verifier</artifactId>
   <version>${okta-jwt.version}</version>
</dependency>

<dependency>
  <groupId>com.okta.jwt</groupId>
  <artifactId>okta-jwt-verifier-impl</artifactId>
  <version>${okta-jwt.version}</version>
  <scope>runtime</scope>
</dependency>

尝试编写测试用例com.okta.jwt.JwtVerificationException: Failed to parse token时出现异常
下面我附上了我的测试用例。
请参考此屏幕截图:My test case.
我的目标是编写100%覆盖的测试用例。

zi8p0yeb

zi8p0yeb1#

大概是这样的

import org.junit.Test;

import static org.junit.Assert.*;

public class JwtVerifierTest {

    @Test
    public void testVerifierBuildAndVerification() {
        String authToken = "valid_auth_token";
        String oktaTokenUrlIssuer = "https://my-okta-instance.com";
        String TOKEN_AUDIENCE = "api://default";

        AccessTokenVerifier jwtVerifier =
                JwtVerifiers.accessTokenVerifierBuilder()
                        .setIssuer(oktaTokenUrlIssuer)
                        .setAudience(TOKEN_AUDIENCE)
                        .setConnectionTimeout(Duration.ofSeconds(1))
                        .build();

        try {
            jwtVerifier.decode(authToken);
            assertTrue(true);
        } catch (Exception e) {
            fail("Verification failed");
        }
    }
}

相关问题