Use RestClient in Spring MVC integration tests
Closes gh-45711
This commit is contained in:
@@ -22,14 +22,12 @@ import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import org.assertj.core.api.InstanceOfAssertFactories;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
|
||||
@@ -56,8 +54,8 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import org.springframework.web.client.RestClient;
|
||||
import org.springframework.web.client.RestClient.RequestHeadersSpec.ExchangeFunction;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -86,12 +84,11 @@ class WebMvcEndpointChildContextConfigurationIntegrationTests {
|
||||
|
||||
@Test // gh-17938
|
||||
void errorEndpointIsUsedWithEndpoint() {
|
||||
this.runner.run(withWebTestClient((client) -> {
|
||||
this.runner.run(withRestClient((client) -> {
|
||||
Map<String, ?> body = client.get()
|
||||
.uri("actuator/fail")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.exchangeToMono(toResponseBody())
|
||||
.block();
|
||||
.exchange(toResponseBody());
|
||||
assertThat(body).hasEntrySatisfying("exception",
|
||||
(value) -> assertThat(value).asString().contains("IllegalStateException"));
|
||||
assertThat(body).hasEntrySatisfying("message",
|
||||
@@ -102,12 +99,11 @@ class WebMvcEndpointChildContextConfigurationIntegrationTests {
|
||||
@Test
|
||||
void errorPageAndErrorControllerIncludeDetails() {
|
||||
this.runner.withPropertyValues("server.error.include-stacktrace=always", "server.error.include-message=always")
|
||||
.run(withWebTestClient((client) -> {
|
||||
.run(withRestClient((client) -> {
|
||||
Map<String, ?> body = client.get()
|
||||
.uri("actuator/fail")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.exchangeToMono(toResponseBody())
|
||||
.block();
|
||||
.exchange(toResponseBody());
|
||||
assertThat(body).hasEntrySatisfying("message",
|
||||
(value) -> assertThat(value).asString().contains("Epic Fail"));
|
||||
assertThat(body).hasEntrySatisfying("trace",
|
||||
@@ -117,12 +113,11 @@ class WebMvcEndpointChildContextConfigurationIntegrationTests {
|
||||
|
||||
@Test
|
||||
void errorEndpointIsUsedWithRestControllerEndpoint() {
|
||||
this.runner.run(withWebTestClient((client) -> {
|
||||
this.runner.run(withRestClient((client) -> {
|
||||
Map<String, ?> body = client.get()
|
||||
.uri("actuator/failController")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.exchangeToMono(toResponseBody())
|
||||
.block();
|
||||
.exchange(toResponseBody());
|
||||
assertThat(body).hasEntrySatisfying("exception",
|
||||
(value) -> assertThat(value).asString().contains("IllegalStateException"));
|
||||
assertThat(body).hasEntrySatisfying("message",
|
||||
@@ -132,13 +127,12 @@ class WebMvcEndpointChildContextConfigurationIntegrationTests {
|
||||
|
||||
@Test
|
||||
void errorEndpointIsUsedWithRestControllerEndpointOnBindingError() {
|
||||
this.runner.run(withWebTestClient((client) -> {
|
||||
this.runner.run(withRestClient((client) -> {
|
||||
Map<String, ?> body = client.post()
|
||||
.uri("actuator/failController")
|
||||
.bodyValue(Collections.singletonMap("content", ""))
|
||||
.body(Collections.singletonMap("content", ""))
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.exchangeToMono(toResponseBody())
|
||||
.block();
|
||||
.exchange(toResponseBody());
|
||||
assertThat(body).hasEntrySatisfying("exception",
|
||||
(value) -> assertThat(value).asString().contains("MethodArgumentNotValidException"));
|
||||
assertThat(body).hasEntrySatisfying("message",
|
||||
@@ -150,12 +144,12 @@ class WebMvcEndpointChildContextConfigurationIntegrationTests {
|
||||
|
||||
@Test
|
||||
void whenManagementServerBasePathIsConfiguredThenEndpointsAreBeneathThatPath() {
|
||||
this.runner.withPropertyValues("management.server.base-path:/manage").run(withWebTestClient((client) -> {
|
||||
this.runner.withPropertyValues("management.server.base-path:/manage").run(withRestClient((client) -> {
|
||||
String body = client.get()
|
||||
.uri("manage/actuator/success")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.exchangeToMono((response) -> response.bodyToMono(String.class))
|
||||
.block();
|
||||
.retrieve()
|
||||
.body(String.class);
|
||||
assertThat(body).isEqualTo("Success");
|
||||
}));
|
||||
}
|
||||
@@ -182,16 +176,16 @@ class WebMvcEndpointChildContextConfigurationIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
private ContextConsumer<AssertableWebApplicationContext> withWebTestClient(Consumer<WebClient> webClient) {
|
||||
private ContextConsumer<AssertableWebApplicationContext> withRestClient(Consumer<RestClient> restClient) {
|
||||
return (context) -> {
|
||||
String port = context.getEnvironment().getProperty("local.management.port");
|
||||
WebClient client = WebClient.create("http://localhost:" + port);
|
||||
webClient.accept(client);
|
||||
RestClient client = RestClient.create("http://localhost:" + port);
|
||||
restClient.accept(client);
|
||||
};
|
||||
}
|
||||
|
||||
private Function<ClientResponse, ? extends Mono<Map<String, ?>>> toResponseBody() {
|
||||
return ((clientResponse) -> clientResponse.bodyToMono(new ParameterizedTypeReference<Map<String, ?>>() {
|
||||
private ExchangeFunction<Map<String, ?>> toResponseBody() {
|
||||
return ((request, response) -> response.bodyTo(new ParameterizedTypeReference<Map<String, ?>>() {
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user