Gee
06/20/2023, 10:29 AMOleg Nenashev
06/20/2023, 10:32 AMwiremockServer.getUrl(String relative)
method for the majority of operations in your test clientsGee
06/20/2023, 10:45 AMimport com.github.tomakehurst.wiremock.client.WireMock;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import static org.assertj.core.api.Assertions.assertThat;
@Testcontainers
public class WireMockContainerPOC {
@Container
public GenericContainer<?> wireMockContainer = new GenericContainer<>(DockerImageName.parse("rodolpheche/wiremock:2.27.2"))
.withExposedPorts(8080);
@Test
public void testStubbedAPI() {
String wireMockBaseUrl = "http://" + wireMockContainer.getHost() + ":" + wireMockContainer.getMappedPort(8080);
WireMock.stubFor(
WireMock.get(WireMock.urlEqualTo("/api/endpoint"))
.willReturn(WireMock.aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{\"message\": \"Stubbed Response\"}"))
);
// Make a request to the stubbed API
String response = wireMockContainer.getHost() + ":" + wireMockContainer.getMappedPort(8080) + "/api/endpoint";
// Assertions
assertThat(response).isNotNull();
assertThat(response).isEqualTo("{\"message\": \"Stubbed Response\"}");
}
}
Oleg Nenashev
06/20/2023, 10:50 AMGee
06/20/2023, 10:58 AMOleg Nenashev
06/20/2023, 10:59 AMGee
06/20/2023, 11:03 AMOleg Nenashev
06/20/2023, 11:06 AMdocker run
Gee
06/20/2023, 11:08 AMResource WireMockContainerTest/hello-world.json relative to org.wiremock.integrations.testcontainers.WireMockContainerTest not found