如何模拟junit中的httpclient.newhttpclient()

发布于 2025-02-01 08:59:59 字数 1427 浏览 1 评论 0原文

 public String sendAPICall(String portalApiUrl, String reviewAppsAuthKey) {

    try {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(new URI(portalApiUrl + CHECK_ORG_ATT_EXP))
                .setHeader(AUTH_REVIEW_API_KEY, reviewAppsAuthKey)
                .setHeader(CONTENT_TYPE, APPLICATION_JSON)
                .GET()
                .build();

        HttpClient httpClient = HttpClient.newHttpClient();

        HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
        if (response.statusCode() == HttpStatus.OK.value()) {
            LOGGER.info(API_SUCCESS);
            return API_SUCCESS;
        }
    } catch (URISyntaxException e) {
        LOGGER.error(API_FAIL_URI, e.getMessage());
    } catch (InterruptedException e) {
        LOGGER.error(API_FAIL_INTER, e.getMessage());
    } catch (IOException e) {
        LOGGER.error(API_FAIL_IO, e.getMessage());
    }
    return API_FAILURE;

}

}

junit:

@Test
public void sendAPICallTest() throws IOException, InterruptedException {

    Checkorg test = getCheckOrg();
    String portalApiUrl = JUNIT_PORTAL_URL;
    String reviewAppsAuthKey = JUNIT_AUTH_KEY;
   String message = test.sendAPICall(portalApiUrl, reviewAppsAuthKey);
    assertEquals(Checkorg.API_SUCCESS, message);
}

如何在测试类中模拟HTTP客户端。

谢谢

 public String sendAPICall(String portalApiUrl, String reviewAppsAuthKey) {

    try {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(new URI(portalApiUrl + CHECK_ORG_ATT_EXP))
                .setHeader(AUTH_REVIEW_API_KEY, reviewAppsAuthKey)
                .setHeader(CONTENT_TYPE, APPLICATION_JSON)
                .GET()
                .build();

        HttpClient httpClient = HttpClient.newHttpClient();

        HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
        if (response.statusCode() == HttpStatus.OK.value()) {
            LOGGER.info(API_SUCCESS);
            return API_SUCCESS;
        }
    } catch (URISyntaxException e) {
        LOGGER.error(API_FAIL_URI, e.getMessage());
    } catch (InterruptedException e) {
        LOGGER.error(API_FAIL_INTER, e.getMessage());
    } catch (IOException e) {
        LOGGER.error(API_FAIL_IO, e.getMessage());
    }
    return API_FAILURE;

}

}

Junit :

@Test
public void sendAPICallTest() throws IOException, InterruptedException {

    Checkorg test = getCheckOrg();
    String portalApiUrl = JUNIT_PORTAL_URL;
    String reviewAppsAuthKey = JUNIT_AUTH_KEY;
   String message = test.sendAPICall(portalApiUrl, reviewAppsAuthKey);
    assertEquals(Checkorg.API_SUCCESS, message);
}

How to mock the HTTP Client in Test Class.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏拉图鍀咏恒 2025-02-08 08:59:59

根据您的评论:

如果我们不能模拟

,我们如何测试此方法的成功

您可以使用间谍为此:

@Test
public void sendAPICallTest() throws IOException, InterruptedException {

    Checkorg test = getCheckOrg();
    String portalApiUrl = JUNIT_PORTAL_URL;
    String reviewAppsAuthKey = JUNIT_AUTH_KEY;
    Checkorg mockedTest = Mockito.spy(test);
    Mockito.when(mockedTest.sendAPICall(portalApiUrl,reviewAppsAuthKey)).thenReturn("API Call Success");
    String message = mockedTest.sendAPICall(portalApiUrl, reviewAppsAuthKey);
    assertEquals(Checkorg.API_SUCCESS, message);
}

如果httpclient模拟不可行,这可能是一个快速的解决方法。

As per your comment:

how can we test this method for success if we can't mock

you can use spy for that :

@Test
public void sendAPICallTest() throws IOException, InterruptedException {

    Checkorg test = getCheckOrg();
    String portalApiUrl = JUNIT_PORTAL_URL;
    String reviewAppsAuthKey = JUNIT_AUTH_KEY;
    Checkorg mockedTest = Mockito.spy(test);
    Mockito.when(mockedTest.sendAPICall(portalApiUrl,reviewAppsAuthKey)).thenReturn("API Call Success");
    String message = mockedTest.sendAPICall(portalApiUrl, reviewAppsAuthKey);
    assertEquals(Checkorg.API_SUCCESS, message);
}

This can be a quick workaround if HttpClient mocking is not feasible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文