我想测试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 对象?
答案 0 :(得分: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);
}