WireMock

By | April 18, 2024
Share the joy
  •  
  •  
  •  
  •  
  •  
  •  

Some code for WireMock.

@Test
public void test() throws Exception {
    String url = "http://localhost:8080/v3";

    stubFor(get(urlPathEqualTo("/v3")).willReturn(aResponse()
            .withHeader("Content-Type", "application/json")
            .withBody("{ \"id\": 123, \"name\": \"John Doe\" }")
            .withStatus(200)));

    stubFor(get(urlPathEqualTo("/v4")).willReturn(aResponse()
            .withHeader("Content-Type", "application/json")
            .withBody("{ \"id\": 123321, \"name\": \"John Doe\" }")
            .withStatus(200)));

    stubFor(get(urlPathEqualTo("/v3"))
            .withQueryParam("abc", equalTo("5"))
            .willReturn(aResponse()
            .withHeader("Content-Type", "application/json")
            .withBody("{ \"id\": v33, \"name\": \"John Doe\" }")
            .withStatus(200)
    ));

    HttpClient client = HttpClient.newHttpClient();

    HttpRequest request = HttpRequest.newBuilder()
            .GET()
            .uri(URI.create(url))
            .build();

    HttpResponse<String> responseFuture = client.send(request, HttpResponse.BodyHandlers.ofString());
    String responseBody = responseFuture.body().get();

}