2017-04-26 50 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); 
}