如何编写具有代码覆盖率的单元测试 - Spring @Bean

时间:2021-06-04 12:44:23

标签: java spring msal

我有一个带有 bean 的配置类

 @Bean
   public static IAuthenticationResult getAccessTokenByClientCredentialGrant() throws Exception {

        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(Files.readAllBytes(Paths.get(KeyPath)));
        PrivateKey key = KeyFactory.getInstance("RSA").generatePrivate(spec);

        InputStream certStream = new ByteArrayInputStream(Files.readAllBytes(Paths.get(CertPath)));
        X509Certificate cert = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(certStream);

         ....

        ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(
                Collections.singleton(scope))
                .build();
        //Need to mock this in Test
        CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
        return future.get();
    }

我想编写一个单元测试来增加我的代码覆盖率。

有一个接近的例子 - https://github.com/AzureAD/microsoft-authentication-library-for-java/blob/dev/src/integrationtest/java/com.microsoft.aad.msal4j/ConfidentialClientApplicationUnitT.java

但我没有使用所有这些最新的库。

我想要带有模拟或 powermock 的简单模型作为获取令牌并且能够创建 bean。请指教

0 个答案:

没有答案
相关问题