WireMock Spring Boot Simplifies the WireMock Configuration for Spring Boot Applications
MMS • Johan Janssen
Article originally posted on InfoQ. Visit InfoQ
WireMock, a flexible tool for building API mocks, and the new WireMock Spring Boot utility simplifies the WireMock configuration for JUnit-based integration tests in Spring Boot applications.
Maciej Walkowiak, freelance architect & developer, released the first version of WireMock Spring Boot in February 2023. The project automatically configures the Spring environment properties and provides a fully declarative WireMock setup. Multiple WireMockServer
instances may be used, one per HTTP client. Lastly, this new utility doesn’t publish extra beans to the Spring application context, but keeps them in a separate store associated with the application context.
WireMock Spring Boot may be used after adding the following Maven dependency:
com.github.maciejwalkowiak.wiremock-spring-boot
wiremock-spring-boot
0.1.0
test
Currently, the dependency is not yet available on Maven Central, but may be used via the JitPack package repository for Git. JitPack downloads the code from the Git repository after the first request and builds the code to provide the build artifacts, such as JAR files. More information can be found in the JitPack documentation.
The following JitPack repository should be configured in the pom.xml, until the artifact is available in Maven Central:
jitpack.io
https://jitpack.io
Tests annotated with @SpringBootTest
, and other annotated tests using the SpringExtension
class, may be annotated with the @EnableWireMock
annotation which enables the WireMockSpringExtension
and adds the test context customizer. The mock is configured with the @ConfigureWireMock
annotation which creates a WireMockServer
and uses the name specified by the property
as the name of an environment property which can be used to retrieve the WireMockServer:
@SpringBootTest
@EnableWireMock({
@ConfigureWireMock(name = "studentservice", property = "studentservice.url")
})
class StudentControllerTest {
@Autowired
private Environment environment;
@WireMock("studentservice")
private WireMockServer wireMockServer;
@Test
void studentTest() {
environment.getProperty("studentservice.url");
wireMockServer.stubFor(get(urlEqualTo("/student"))
…
}
}
The previous example uses the environment.getProperty("studentservice.url")
method to retrieve the URL of the WireMockServer
instance.
WireMock extensions may be configured via the extensions
parameter in the configuration annotation:
@ConfigureWireMock(extensions = { … }, …)
By default, the classpath directory containing the mapping files is set to wiremock/{server-name}/mappings, but may be changed via the stubLocation
parameter in the configuration annotation:
@ConfigureWireMock(stubLocation = "customLocation", …)
Automatically-set Spring properties and the declarative configuration of multiple WireMockServer
instances are advantages of WireMock Spring Boot compared to Spring Cloud Contract WireMock. However, the latter supports contract testing, REST docs and other features.
WireMock Spring Boot uses the concepts and ideas from the Spring Cloud Contract WireMock and Spring Boot WireMock projects and the article Spring Boot Integration Tests With WireMock and JUnit 5. More information about the project can be found on GitHub.