微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我有一个带有 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。请指教

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。