From a0aeea296281ea6333c1f68a7f5f53c39dc31410 Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Thu, 28 Sep 2023 14:17:36 +0200 Subject: [PATCH] Add WireMock Spring Boot to the Solutions page --- _docs/solutions/spring-boot.md | 47 ++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/_docs/solutions/spring-boot.md b/_docs/solutions/spring-boot.md index 2fc22a0e..bb27b622 100644 --- a/_docs/solutions/spring-boot.md +++ b/_docs/solutions/spring-boot.md @@ -1,13 +1,56 @@ --- layout: solution -title: "Using with Spring Boot" +title: "Using WireMock with Spring Boot" meta_title: Running WireMock with Spring Boot | WireMock toc_rank: 116 description: The team behind Spring Cloud Contract have created a library to support running WireMock using the “ambient” HTTP server -redirect_from: "/docs/spring-boot.html" +redirect_from: +- "/docs/spring-boot.html" logo: /images/logos/technology/spring.svg --- +## WireMock Spring Boot + +[WireMock Spring Boot](https://github.com/maciejwalkowiak/wiremock-spring-boot) +simplifies testing HTTP clients in Spring Boot & Junit 5 based integration tests. +It includes fully declarative WireMock setup, +supports multiple `WireMockServer` instances, +automatically sets Spring environment properties, +and does not pollute Spring application context with extra beans. + +Example: + +```java +@SpringBootTest +@EnableWireMock({ + @ConfigureWireMock(name = "user-service", property = "user-client.url") +}) +class TodoControllerTests { + + @InjectWireMock("user-service") + private WireMockServer wiremock; + + @Autowired + private Environment env; + + @Test + void aTest() { + // returns a URL to WireMockServer instance + env.getProperty("user-client.url"); + wiremock.stubFor(stubFor(get("/todolist").willReturn(aResponse() + .withHeader("Content-Type", "application/json") + .withBody(""" + [ + { "id": 1, "userId": 1, "title": "my todo" }, + ] + """) + ))); + } +} +``` + +## Spring Cloud Contract + The team behind Spring Cloud Contract have created a library to support running WireMock using the "ambient" HTTP server. It also simplifies some aspects of configuration and eliminates some common issues that occur when running Spring Boot and WireMock together.