Skip to content

Instantly share code, notes, and snippets.

@bitkill
Created May 11, 2024 23:03
Show Gist options
  • Save bitkill/37d15645d9552c6d2a46acf4d5409a93 to your computer and use it in GitHub Desktop.
Save bitkill/37d15645d9552c6d2a46acf4d5409a93 to your computer and use it in GitHub Desktop.
[java-wiremock-testcontainers] Testcontainers with wiremock from stubs #wiremock #java #testcontainers
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
@Testcontainers
@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class WiremockIT {
private static final int WIREMOCK_PORT = 8080;
@Container
public static final GenericContainer<?> WIREMOCK_PRODUCT = new GenericContainer(
DockerImageName.parse("wiremock/wiremock:3.5.4")
)
.waitingFor(
Wait
.forHttp("/__admin/health")
.withMethod("GET")
.forStatusCode(200)
)
.withCopyFileToContainer(
MountableFile.forClasspathResource("/stubs/example.json"),
"/home/wiremock/mappings/"
)
.withExposedPorts(WIREMOCK_PORT);
@DynamicPropertySource
private static void registerWiremockProductProperties(DynamicPropertyRegistry registry) {
registry.add("api.url", () ->
"http://" + WIREMOCK_PRODUCT.getHost() + ":" + WIREMOCK_PRODUCT.getMappedPort(WIREMOCK_PORT)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment