30 Mart 2023 Perşembe

WireMock WireMockRunner Sınıfı - JUnit4 İçindir

Giriş
Açıklaması şöyle
This will set up a mock server before each test and tear it down after each test.
Örnek
Şöyle yaparız
@RunWith(WireMockRunner.class)
public class MockServerTest {

  @Test
  public void testMockServer() throws Exception {
    stubFor(get(urlEqualTo("/api/endpoint"))
      .willReturn(aResponse()
        .withStatus(200)
        .withHeader("Content-Type", "application/json")
        .withBody("{\"message\": \"Hello, World!\"}")));

    URL url = new URL("http://localhost:8080/api/endpoint");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.connect();

    assertThat(connection.getResponseCode(), is(200));
    assertThat(connection.getHeaderField("Content-Type"), is("application/json"));
    assertThat(new BufferedReader(new InputStreamReader(connection.getInputStream()))
    .lines().collect(Collectors.joining()), is("{\"message\": \"Hello, World!\"}"));
  }
}


Hiç yorum yok:

Yorum Gönder