这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions testing-modules/spring-testing-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@
<version>${hsqldb.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock.integrations</groupId>
<artifactId>wiremock-spring-boot</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.baeldung.wiremock;

import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.okJson;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.wiremock.spring.ConfigureWireMock;
import org.wiremock.spring.EnableWireMock;
import org.wiremock.spring.InjectWireMock;

import com.github.tomakehurst.wiremock.WireMockServer;

@SpringBootTest(classes = SimpleWiremockIntegrationTest.AppConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EnableWireMock({
@ConfigureWireMock(name = "user-service", port = 8081),
@ConfigureWireMock(name = "product-service", port = 8082)
})
public class InjectedWiremockIntegrationTest {

@InjectWireMock("user-service")
WireMockServer mockUserService;

@InjectWireMock("product-service")
WireMockServer mockProductService;

@Autowired
private TestRestTemplate restTemplate;

@Test
void givenEmptyUserList_whenFetchingUsers_thenReturnsEmptyList() {
mockUserService.stubFor(get("/users").willReturn(okJson("[]")));

ResponseEntity<String> response = restTemplate
.getForEntity("http://localhost:8081/users", String.class);

Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
Assertions.assertEquals("[]", response.getBody());
}

@Test
void givenUserAndProductLists_whenFetchingUsersAndProducts_thenReturnsMockedData() {
mockUserService
.stubFor(get("/users")
.willReturn(okJson("[{\"id\": 1, \"name\": \"John\"}]")));
mockProductService
.stubFor(get("/products")
.willReturn(okJson("[{\"id\": 101, \"name\": \"Laptop\"}]")));

ResponseEntity<String> userResponse = restTemplate
.getForEntity("http://localhost:8081/users", String.class);
ResponseEntity<String> productResponse = restTemplate
.getForEntity("http://localhost:8082/products", String.class);

Assertions.assertEquals(HttpStatus.OK, userResponse.getStatusCode());
Assertions.assertEquals("[{\"id\": 1, \"name\": \"John\"}]", userResponse.getBody());

Assertions.assertEquals(HttpStatus.OK, productResponse.getStatusCode());
Assertions.assertEquals("[{\"id\": 101, \"name\": \"Laptop\"}]", productResponse.getBody());
}

@SpringBootApplication
static class AppConfiguration {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.baeldung.wiremock;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.wiremock.spring.EnableWireMock;

@SpringBootTest(classes = SimpleWiremockIntegrationTest.AppConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EnableWireMock
class SimpleWiremockIntegrationTest {

@Value("${wiremock.server.baseUrl}")
private String wireMockUrl;

@Autowired
private TestRestTemplate restTemplate;

@Test
void givenWireMockStub_whenGetPing_thenReturnsPong() {
stubFor(get("/ping").willReturn(ok("pong")));

ResponseEntity<String> response = restTemplate.getForEntity(wireMockUrl + "/ping", String.class);

Assertions.assertEquals("pong", response.getBody());
}

@Test
void givenWireMockStub_whenGetGreeting_thenReturnsMockedJsonResponse() {
String mockResponse = "{\"message\": \"Hello, Baeldung!\"}";
stubFor(get("/api/greeting")
.willReturn(okJson(mockResponse)));

ResponseEntity<String> response = restTemplate.getForEntity(wireMockUrl + "/api/greeting", String.class);

Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
Assertions.assertEquals(mockResponse, response.getBody());
}

@Test
void givenWireMockStub_whenGetUnknownResource_thenReturnsNotFound() {
stubFor(get("/api/unknown").willReturn(aResponse().withStatus(404)));

ResponseEntity<String> response = restTemplate.getForEntity(wireMockUrl + "/api/unknown", String.class);

Assertions.assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}

@SpringBootApplication
static class AppConfiguration {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wiremock.server.baseUrl= http://localhost:8080