WireMock, a versatile device for constructing API mocks, and the brand new WireMock Spring Boot utility simplifies the WireMock configuration for JUnit-based integration assessments in Spring Boot purposes.
Maciej Walkowiak, freelance architect & developer, launched the primary model of WireMock Spring Boot in February 2023. The challenge routinely configures the Spring atmosphere properties and offers a completely declarative WireMock setup. A number of WireMockServer
cases could also be used, one per HTTP consumer. Lastly, this new utility would not publish additional beans to the Spring software context, however retains them in a separate retailer related to the appliance context.
WireMock Spring Boot could also be used after including the next Maven dependency:
<dependency>
<groupId>com.github.maciejwalkowiak.wiremock-spring-boot</groupId>
<artifactId>wiremock-spring-boot</artifactId>
<model>0.1.0</model>
<scope>take a look at</scope>
</dependency>
At the moment, the dependency is just not but out there on Maven Central, however could also be used through the JitPack package deal repository for Git. JitPack downloads the code from the Git repository after the primary request and builds the code to supply the construct artifacts, reminiscent of JAR recordsdata. Extra data could be discovered within the JitPack documentation.
The next JitPack repository needs to be configured within the pom.xml, till the artifact is obtainable in Maven Central:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Exams annotated with @SpringBootTest
, and different annotated assessments utilizing the SpringExtension
class, could also be annotated with the @EnableWireMock
annotation which allows the WireMockSpringExtension
and provides the take a look at context customizer. The mock is configured with the @ConfigureWireMock
annotation which creates a WireMockServer
and makes use of the title specified by the property
because the title of an atmosphere property which can be utilized to retrieve the WireMockServer:
@SpringBootTest
@EnableWireMock({
@ConfigureWireMock(title = "studentservice", property = "studentservice.url")
})
class StudentControllerTest {
@Autowired
personal Setting atmosphere;
@WireMock("studentservice")
personal WireMockServer wireMockServer;
@Check
void studentTest() {
atmosphere.getProperty("studentservice.url");
wireMockServer.stubFor(get(urlEqualTo("/scholar"))
…
}
}
The earlier instance makes use of the atmosphere.getProperty("studentservice.url")
methodology to retrieve the URL of the WireMockServer
occasion.
WireMock extensions could also be configured through the extensions
parameter within the configuration annotation:
@ConfigureWireMock(extensions = { … }, …)
By default, the classpath listing containing the mapping recordsdata is ready to wiremock/{server-name}/mappings, however could also be modified through the stubLocation
parameter within the configuration annotation:
@ConfigureWireMock(stubLocation = "customLocation", …)
Routinely-set Spring properties and the declarative configuration of a number of WireMockServer
cases are benefits of WireMock Spring Boot in comparison with Spring Cloud Contract WireMock. Nevertheless, the latter helps contract testing, REST docs and different options.
WireMock Spring Boot makes use of the ideas and concepts from the Spring Cloud Contract WireMock and Spring Boot WireMock initiatives and the article Spring Boot Integration Exams With WireMock and JUnit 5. Extra details about the challenge could be discovered on GitHub.