WireMock Spring Boot library drastically simplifies WireMock configuration in a Spring Boot and JUnit 5 application.
- Fully declarative WireMock setup
- Support for multiple
WireMockServer
instances - one per HTTP client as recommended in the WireMock documentation - Automatically sets Spring environment properties
- Does not pollute Spring application context with extra beans
It is originally forked from WireMock Spring Boot.
Add the dependency to wiremock-spring-boot
:
<dependency>
<groupId>org.wiremock.integrations</groupId>
<artifactId>wiremock-spring-boot</artifactId>
<version>X</version>
<scope>test</scope>
</dependency>
testImplementation "org.wiremock.integrations:wiremock-spring-boot:X"
Enable it with the @EnableWireMock
annotation, like:
@SpringBootTest
@EnableWireMock
class DefaultPropertiesTest {
@Value("${wiremock.server.baseUrl}")
private String wiremockUrl;
@Value("${wiremock.server.port}")
private String wiremockPort;
@BeforeEach
public void before() {
WireMock.stubFor(get("/the_default_prop_mock").willReturn(aResponse().withStatus(202)));
}
@Test
void test() {
RestAssured.baseURI = this.wiremockUrl;
RestAssured.when().get("/the_default_prop_mock").then().statusCode(202);
}
}
There are more running examples in the repo.
@EnableWireMock
adds test context customizer and enablesWireMockSpringExtension
.@ConfigureWireMock
creates aWireMockServer
.@InjectWireMock
injectsWireMockServer
instance to a test.
By default these will be provided:
wiremock.server.baseUrl
- Base URL of WireMock server.wiremock.server.port
- HTTP port of WireMock server.
These can be changed with:
@EnableWireMock(
@ConfigureWireMock(
baseUrlProperties = { "customUrl", "sameCustomUrl" },
portProperties = "customPort"))
class CustomPropertiesTest {
@Value("${customUrl}")
private String customUrl;
@Value("${sameCustomUrl}")
private String sameCustomUrl;
@Value("${customPort}")
private String customPort;
By default, each WireMockServer
is configured to load WireMock root from:
- Classpath if specified
{specified-resource-name}/{server-name}
{specified-resource-name}
- Directory
{CWD}/wiremock/{server-name}
{CWD}/stubs/{server-name}
{CWD}/mappings/{server-name}
{CWD}/wiremock
{CWD}/stubs
{CWD}/mappings
It can be changed:
@EnableWireMock({
@ConfigureWireMock(
name = "fs-client",
filesUnderClasspath = "some/classpath/resource",
filesUnderDirectory = "or/a/directory")
})
WireMock extensions can be registered independently with each @ConfigureWireMock
:
@EnableWireMock({
@ConfigureWireMock(extensions = { ... })
})
- Maciej Walkowiak - This was originally his project and later moved to WireMock organization
- Spring Cloud Contract WireMock
- Spring Boot WireMock
- Spring Boot Integration Tests With WireMock and JUnit 5 by Philip Riecks