2017-04-26 49 views
0

我想测试http请求/响应。所以我使用WireMock。WireMock:存根 - 如何获取对象“testClient”?

我想存根特定请求响应: 下面的代码:

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

testClient是您嘲笑API的客户端库。

看起来像你从示例中直接复制,仅仅是指示性的。

用您选择的HTTP库替换testClient,例如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); 
}