2017-04-26 25 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のクライアントライブラリです。

あなたは参考になる例から直接コピーしたようです。

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); 
} 
関連する問題