From 2bdbc1f8d2375ac206eea320687acac89badbf24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Wed, 28 May 2025 14:44:22 +0200 Subject: [PATCH] Use RestClient in Spring MVC integration tests Closes gh-45711 --- ...dContextConfigurationIntegrationTests.java | 44 ++++++++----------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/WebMvcEndpointChildContextConfigurationIntegrationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/WebMvcEndpointChildContextConfigurationIntegrationTests.java index 8d014fbf38f..22b1bb027d3 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/WebMvcEndpointChildContextConfigurationIntegrationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/WebMvcEndpointChildContextConfigurationIntegrationTests.java @@ -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 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 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 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 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 withWebTestClient(Consumer webClient) { + private ContextConsumer withRestClient(Consumer 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>> toResponseBody() { - return ((clientResponse) -> clientResponse.bodyToMono(new ParameterizedTypeReference>() { + private ExchangeFunction> toResponseBody() { + return ((request, response) -> response.bodyTo(new ParameterizedTypeReference>() { })); }