[フレーム]
BT

InfoQ Software Architects' Newsletter

A monthly overview of things you need to know as an architect or aspiring architect.

View an example

We protect your privacy.

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Unlock the full InfoQ experience

Unlock the full InfoQ experience by logging in! Stay updated with your favorite authors and topics, engage with content, and download exclusive resources.

Log In
or

Don't have an InfoQ account?

Register
  • Stay updated on topics and peers that matter to youReceive instant alerts on the latest insights and trends.
  • Quickly access free resources for continuous learningMinibooks, videos with transcripts, and training materials.
  • Save articles and read at anytimeBookmark articles to read whenever youre ready.

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

Mar 08, 2023 2 min read

Write for InfoQ

Feed your curiosity. Help 550k+ global
senior developers
each month stay ahead.
Get in touch

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

Johan Janssen

Show moreShow less

Rate this Article

Adoption
Style

This content is in the Java topic

Related Topics:

Related Content

The InfoQ Newsletter

A round-up of last week’s content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers. View an example

We protect your privacy.

BT

AltStyle によって変換されたページ (->オリジナル) /