BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News WireMock Spring Boot Simplifies the WireMock Configuration for Spring Boot Applications

WireMock Spring Boot Simplifies the WireMock Configuration for Spring Boot Applications

Bookmarks

WireMock, a flexible tool for building API mocks, and the new WireMock Spring Boot utility simplify 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:

<dependency>
    <groupId>com.github.maciejwalkowiak.wiremock-spring-boot</groupId>
    <artifactId>wiremock-spring-boot</artifactId>
    <version>0.1.0</version>
    <scope>test</scope>
</dependency>

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:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

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.

About the Author

Rate this Article

Adoption
Style

BT