如何使用Wiremock存根URL?

时间:2019-10-31 10:54:35

标签: spring-boot junit mockito stub wiremock

我正在尝试模拟第三方URL,成功模拟了它,但没有涵盖服务代码:

服务方法:

 public void serviceConnection(){
 System.out.println("Hello in service");
 baseURL = new URL("http://demo.live.com");
 HttpURLConnection conn = (HttpURLConnection) baseURL.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    conn.setRequestProperty(CONTENT_TYPE, ACCEPT_CONTENT_TYPE_VAL);

    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
    wr.writeBytes(requestBody);
    wr.flush();
    wr.close();
 }

测试用例的方法:

 public void testConnection(){

 wireMockServer.start();
 configureFor("localhost", 8080);

 stubFor(get(urlEqualTo("/api/auth/test")).willReturn(aResponse().withBody("Hello World for user side.")));

 HttpGet request = new HttpGet("http://localhost:8080/api/auth/test");

 CloseableHttpClient httpClient = HttpClients.createDefault();

 HttpResponse httpResponse = httpClient.execute(request);

 String stringResponse = convertResponseToString(httpResponse);

 verify(getRequestedFor(urlEqualTo("/api/auth/test")));

 assertEquals("Hello World for user side.", stringResponse);
 }

因此,我无法获得所需的代码覆盖率。 仅供参考:我正在使用问题进行代码覆盖率检查。

0 个答案:

没有答案