我想测试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));
}
Run Code Online (Sandbox Code Playgroud)
代码未编译,因为没有对象testClient。如何获取testClient对象?
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1485 次 |
| 最近记录: |