【问题标题】:WireMock: Stubbing - How get object "testClient"?WireMock:存根 - 如何获取对象“testClient”?
【发布时间】:2017-04-26 16:35:07
【问题描述】:

我想测试 http 请求/响应。所以我使用了 WireMock。

我想为特定请求存根响应: 这里代码:

 public class WireMockPersons {

    @Rule
    public WireMockRule wireMockRule = new WireMockRule(8089);

    @Test
public void exactUrlOnly() {
    stubFor(get(urlEqualTo("/some/thing"))
            .willReturn(aResponse()
                .withHeader("Content-Type", "text/plain")
                .withBody("Hello world!")));

    assertThat(testClient.get("/some/thing").statusCode(), is(200));
    assertThat(testClient.get("/some/thing/else").statusCode(), is(404));
}

代码无法编译,因为没有对象 testClient。如何获取 testClient 对象?

【问题讨论】:

    标签: wiremock


    【解决方案1】:

    testClient 是您正在模拟的 API 的客户端库。

    看起来您直接从示例中复制了这些示例,这些示例仅供参考。

    将testClient 替换为您选择的HTTP 库,例如HttpClient。

    String url = "http://localhost:8089/some/thing";
    try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
        HttpGet get = new HttpGet(url);
        HttpEntity entity = client.execute(get).getEntity();
        return  EntityUtils.toString(entity, "UTF-8");
    } catch (IOException e) {
        throw new RuntimeException("Unable to call " + url, e);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 2014-07-13
      • 2017-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多