Skip to content

Commit bc47cb2

Browse files
BAEL-9036 - Integrating WireMock with Spring Boot (#18403)
* wiremock working test * wiremock tests * fix property file * fix test names
1 parent ea78a2a commit bc47cb2

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

testing-modules/spring-testing-2/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@
6666
<version>${hsqldb.version}</version>
6767
<scope>test</scope>
6868
</dependency>
69+
<dependency>
70+
<groupId>org.wiremock.integrations</groupId>
71+
<artifactId>wiremock-spring-boot</artifactId>
72+
<version>2.2.0</version>
73+
<scope>test</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>com.fasterxml.jackson.core</groupId>
77+
<artifactId>jackson-core</artifactId>
78+
<version>${jackson.version}</version>
79+
</dependency>
80+
<dependency>
81+
<groupId>com.fasterxml.jackson.core</groupId>
82+
<artifactId>jackson-databind</artifactId>
83+
<version>${jackson.version}</version>
84+
</dependency>
85+
6986
</dependencies>
7087

7188
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.wiremock;
2+
3+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
4+
import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
5+
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.boot.test.web.client.TestRestTemplate;
12+
import org.springframework.http.HttpStatus;
13+
import org.springframework.http.ResponseEntity;
14+
import org.wiremock.spring.ConfigureWireMock;
15+
import org.wiremock.spring.EnableWireMock;
16+
import org.wiremock.spring.InjectWireMock;
17+
18+
import com.github.tomakehurst.wiremock.WireMockServer;
19+
20+
@SpringBootTest(classes = SimpleWiremockIntegrationTest.AppConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
21+
@EnableWireMock({
22+
@ConfigureWireMock(name = "user-service", port = 8081),
23+
@ConfigureWireMock(name = "product-service", port = 8082)
24+
})
25+
public class InjectedWiremockIntegrationTest {
26+
27+
@InjectWireMock("user-service")
28+
WireMockServer mockUserService;
29+
30+
@InjectWireMock("product-service")
31+
WireMockServer mockProductService;
32+
33+
@Autowired
34+
private TestRestTemplate restTemplate;
35+
36+
@Test
37+
void givenEmptyUserList_whenFetchingUsers_thenReturnsEmptyList() {
38+
mockUserService.stubFor(get("/users").willReturn(okJson("[]")));
39+
40+
ResponseEntity<String> response = restTemplate
41+
.getForEntity("http://localhost:8081/users", String.class);
42+
43+
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
44+
Assertions.assertEquals("[]", response.getBody());
45+
}
46+
47+
@Test
48+
void givenUserAndProductLists_whenFetchingUsersAndProducts_thenReturnsMockedData() {
49+
mockUserService
50+
.stubFor(get("/users")
51+
.willReturn(okJson("[{\"id\": 1, \"name\": \"John\"}]")));
52+
mockProductService
53+
.stubFor(get("/products")
54+
.willReturn(okJson("[{\"id\": 101, \"name\": \"Laptop\"}]")));
55+
56+
ResponseEntity<String> userResponse = restTemplate
57+
.getForEntity("http://localhost:8081/users", String.class);
58+
ResponseEntity<String> productResponse = restTemplate
59+
.getForEntity("http://localhost:8082/products", String.class);
60+
61+
Assertions.assertEquals(HttpStatus.OK, userResponse.getStatusCode());
62+
Assertions.assertEquals("[{\"id\": 1, \"name\": \"John\"}]", userResponse.getBody());
63+
64+
Assertions.assertEquals(HttpStatus.OK, productResponse.getStatusCode());
65+
Assertions.assertEquals("[{\"id\": 101, \"name\": \"Laptop\"}]", productResponse.getBody());
66+
}
67+
68+
@SpringBootApplication
69+
static class AppConfiguration {}
70+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.wiremock;
2+
3+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
4+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
5+
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
6+
import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
7+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
8+
9+
import org.junit.jupiter.api.Assertions;
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.beans.factory.annotation.Value;
13+
import org.springframework.boot.autoconfigure.SpringBootApplication;
14+
import org.springframework.boot.test.context.SpringBootTest;
15+
import org.springframework.boot.test.web.client.TestRestTemplate;
16+
import org.springframework.http.HttpStatus;
17+
import org.springframework.http.ResponseEntity;
18+
import org.wiremock.spring.EnableWireMock;
19+
20+
@SpringBootTest(classes = SimpleWiremockIntegrationTest.AppConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
21+
@EnableWireMock
22+
class SimpleWiremockIntegrationTest {
23+
24+
@Value("${wiremock.server.baseUrl}")
25+
private String wireMockUrl;
26+
27+
@Autowired
28+
private TestRestTemplate restTemplate;
29+
30+
@Test
31+
void givenWireMockStub_whenGetPing_thenReturnsPong() {
32+
stubFor(get("/ping").willReturn(ok("pong")));
33+
34+
ResponseEntity<String> response = restTemplate.getForEntity(wireMockUrl + "/ping", String.class);
35+
36+
Assertions.assertEquals("pong", response.getBody());
37+
}
38+
39+
@Test
40+
void givenWireMockStub_whenGetGreeting_thenReturnsMockedJsonResponse() {
41+
String mockResponse = "{\"message\": \"Hello, Baeldung!\"}";
42+
stubFor(get("/api/greeting")
43+
.willReturn(okJson(mockResponse)));
44+
45+
ResponseEntity<String> response = restTemplate.getForEntity(wireMockUrl + "/api/greeting", String.class);
46+
47+
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
48+
Assertions.assertEquals(mockResponse, response.getBody());
49+
}
50+
51+
@Test
52+
void givenWireMockStub_whenGetUnknownResource_thenReturnsNotFound() {
53+
stubFor(get("/api/unknown").willReturn(aResponse().withStatus(404)));
54+
55+
ResponseEntity<String> response = restTemplate.getForEntity(wireMockUrl + "/api/unknown", String.class);
56+
57+
Assertions.assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
58+
}
59+
60+
@SpringBootApplication
61+
static class AppConfiguration {}
62+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
wiremock.server.baseUrl= http://localhost:8080

0 commit comments

Comments
 (0)