diff --git a/module/spring-boot-devtools/src/intTest/java/org/springframework/boot/devtools/tests/DevToolsIntegrationTests.java b/module/spring-boot-devtools/src/intTest/java/org/springframework/boot/devtools/tests/DevToolsIntegrationTests.java index 5d897df5fbb..6e0bd2da730 100644 --- a/module/spring-boot-devtools/src/intTest/java/org/springframework/boot/devtools/tests/DevToolsIntegrationTests.java +++ b/module/spring-boot-devtools/src/intTest/java/org/springframework/boot/devtools/tests/DevToolsIntegrationTests.java @@ -25,10 +25,10 @@ import org.apache.hc.core5.util.TimeValue; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; -import org.springframework.boot.restclient.RestTemplateBuilder; -import org.springframework.boot.resttestclient.TestRestTemplate; -import org.springframework.http.HttpStatus; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.test.web.servlet.client.RestTestClient; +import org.springframework.test.web.servlet.client.RestTestClient.BodySpec; +import org.springframework.test.web.servlet.client.StatusAssertions; import static org.assertj.core.api.Assertions.assertThat; @@ -40,23 +40,23 @@ import static org.assertj.core.api.Assertions.assertThat; @SuppressWarnings("removal") class DevToolsIntegrationTests extends AbstractDevToolsIntegrationTests { - private final TestRestTemplate template = new TestRestTemplate(new RestTemplateBuilder() - .requestFactory(() -> new HttpComponentsClientHttpRequestFactory(HttpClients.custom() + private final RestTestClient client = RestTestClient + .bindToServer(new HttpComponentsClientHttpRequestFactory(HttpClients.custom() .setRetryStrategy(new DefaultHttpRequestRetryStrategy(10, TimeValue.of(1, TimeUnit.SECONDS))) - .build()))); + .build())) + .build(); @ParameterizedTest(name = "{0}") @MethodSource("parameters") void addARequestMappingToAnExistingController(ApplicationLauncher applicationLauncher) throws Exception { launchApplication(applicationLauncher, "--logging.level.org.springframework.boot=trace"); String urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForObject(urlBase + "/one", String.class)).isEqualTo("one"); - assertThat(this.template.getForEntity(urlBase + "/two", String.class).getStatusCode()) - .isEqualTo(HttpStatus.NOT_FOUND); + expectResponseBody(urlBase + "/one").isEqualTo("one"); + expectResponseStatus(urlBase + "/two").isNotFound(); controller("com.example.ControllerOne").withRequestMapping("one").withRequestMapping("two").build(); urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForObject(urlBase + "/one", String.class)).isEqualTo("one"); - assertThat(this.template.getForObject(urlBase + "/two", String.class)).isEqualTo("two"); + expectResponseBody(urlBase + "/one").isEqualTo("one"); + expectResponseBody(urlBase + "/two").isEqualTo("two"); } @ParameterizedTest(name = "{0}") @@ -64,11 +64,10 @@ class DevToolsIntegrationTests extends AbstractDevToolsIntegrationTests { void removeARequestMappingFromAnExistingController(ApplicationLauncher applicationLauncher) throws Exception { launchApplication(applicationLauncher); String urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForObject(urlBase + "/one", String.class)).isEqualTo("one"); + expectResponseBody(urlBase + "/one").isEqualTo("one"); controller("com.example.ControllerOne").build(); urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForEntity(urlBase + "/one", String.class).getStatusCode()) - .isEqualTo(HttpStatus.NOT_FOUND); + expectResponseStatus(urlBase + "/one").isNotFound(); } @ParameterizedTest(name = "{0}") @@ -76,13 +75,12 @@ class DevToolsIntegrationTests extends AbstractDevToolsIntegrationTests { void createAController(ApplicationLauncher applicationLauncher) throws Exception { launchApplication(applicationLauncher); String urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForObject(urlBase + "/one", String.class)).isEqualTo("one"); - assertThat(this.template.getForEntity(urlBase + "/two", String.class).getStatusCode()) - .isEqualTo(HttpStatus.NOT_FOUND); + expectResponseBody(urlBase + "/one").isEqualTo("one"); + expectResponseStatus(urlBase + "/two").isNotFound(); controller("com.example.ControllerTwo").withRequestMapping("two").build(); urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForObject(urlBase + "/one", String.class)).isEqualTo("one"); - assertThat(this.template.getForObject(urlBase + "/two", String.class)).isEqualTo("two"); + expectResponseBody(urlBase + "/one").isEqualTo("one"); + expectResponseBody(urlBase + "/two").isEqualTo("two"); } @@ -91,16 +89,15 @@ class DevToolsIntegrationTests extends AbstractDevToolsIntegrationTests { void createAControllerAndThenAddARequestMapping(ApplicationLauncher applicationLauncher) throws Exception { launchApplication(applicationLauncher); String urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForObject(urlBase + "/one", String.class)).isEqualTo("one"); - assertThat(this.template.getForEntity(urlBase + "/two", String.class).getStatusCode()) - .isEqualTo(HttpStatus.NOT_FOUND); + expectResponseBody(urlBase + "/one").isEqualTo("one"); + expectResponseStatus(urlBase + "/two").isNotFound(); controller("com.example.ControllerTwo").withRequestMapping("two").build(); urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForObject(urlBase + "/one", String.class)).isEqualTo("one"); - assertThat(this.template.getForObject(urlBase + "/two", String.class)).isEqualTo("two"); + expectResponseBody(urlBase + "/one").isEqualTo("one"); + expectResponseBody(urlBase + "/two").isEqualTo("two"); controller("com.example.ControllerTwo").withRequestMapping("two").withRequestMapping("three").build(); urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForObject(urlBase + "/three", String.class)).isEqualTo("three"); + expectResponseBody(urlBase + "/three").isEqualTo("three"); } @ParameterizedTest(name = "{0}") @@ -109,18 +106,17 @@ class DevToolsIntegrationTests extends AbstractDevToolsIntegrationTests { throws Exception { launchApplication(applicationLauncher); String urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForObject(urlBase + "/one", String.class)).isEqualTo("one"); - assertThat(this.template.getForEntity(urlBase + "/two", String.class).getStatusCode()) - .isEqualTo(HttpStatus.NOT_FOUND); + expectResponseBody(urlBase + "/one").isEqualTo("one"); + expectResponseStatus(urlBase + "/two").isNotFound(); controller("com.example.ControllerTwo").withRequestMapping("two").build(); urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForObject(urlBase + "/one", String.class)).isEqualTo("one"); - assertThat(this.template.getForObject(urlBase + "/two", String.class)).isEqualTo("two"); + expectResponseBody(urlBase + "/one").isEqualTo("one"); + expectResponseBody(urlBase + "/two").isEqualTo("two"); controller("com.example.ControllerOne").withRequestMapping("one").withRequestMapping("three").build(); urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForObject(urlBase + "/one", String.class)).isEqualTo("one"); - assertThat(this.template.getForObject(urlBase + "/two", String.class)).isEqualTo("two"); - assertThat(this.template.getForObject(urlBase + "/three", String.class)).isEqualTo("three"); + expectResponseBody(urlBase + "/one").isEqualTo("one"); + expectResponseBody(urlBase + "/two").isEqualTo("two"); + expectResponseBody(urlBase + "/three").isEqualTo("three"); } @ParameterizedTest(name = "{0}") @@ -128,12 +124,11 @@ class DevToolsIntegrationTests extends AbstractDevToolsIntegrationTests { void deleteAController(ApplicationLauncher applicationLauncher) throws Exception { LaunchedApplication launchedApplication = launchApplication(applicationLauncher); String urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForObject(urlBase + "/one", String.class)).isEqualTo("one"); + expectResponseBody(urlBase + "/one").isEqualTo("one"); assertThat(new File(launchedApplication.getClassesDirectory(), "com/example/ControllerOne.class").delete()) .isTrue(); urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForEntity(urlBase + "/one", String.class).getStatusCode()) - .isEqualTo(HttpStatus.NOT_FOUND); + expectResponseStatus(urlBase + "/one").isNotFound(); } @@ -142,18 +137,16 @@ class DevToolsIntegrationTests extends AbstractDevToolsIntegrationTests { void createAControllerAndThenDeleteIt(ApplicationLauncher applicationLauncher) throws Exception { LaunchedApplication launchedApplication = launchApplication(applicationLauncher); String urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForObject(urlBase + "/one", String.class)).isEqualTo("one"); - assertThat(this.template.getForEntity(urlBase + "/two", String.class).getStatusCode()) - .isEqualTo(HttpStatus.NOT_FOUND); + expectResponseBody(urlBase + "/one").isEqualTo("one"); + expectResponseStatus(urlBase + "/two").isNotFound(); controller("com.example.ControllerTwo").withRequestMapping("two").build(); urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForObject(urlBase + "/one", String.class)).isEqualTo("one"); - assertThat(this.template.getForObject(urlBase + "/two", String.class)).isEqualTo("two"); + expectResponseBody(urlBase + "/one").isEqualTo("one"); + expectResponseBody(urlBase + "/two").isEqualTo("two"); assertThat(new File(launchedApplication.getClassesDirectory(), "com/example/ControllerTwo.class").delete()) .isTrue(); urlBase = "http://localhost:" + awaitServerPort(); - assertThat(this.template.getForEntity(urlBase + "/two", String.class).getStatusCode()) - .isEqualTo(HttpStatus.NOT_FOUND); + expectResponseStatus(urlBase + "/two").isNotFound(); } static Object[] parameters() { @@ -163,4 +156,12 @@ class DevToolsIntegrationTests extends AbstractDevToolsIntegrationTests { new Object[] { new JarFileRemoteApplicationLauncher(directories) } }; } + private BodySpec expectResponseBody(String url) { + return this.client.get().uri(url).exchangeSuccessfully().expectBody(String.class); + } + + private StatusAssertions expectResponseStatus(String url) { + return this.client.get().uri(url).exchange().expectStatus(); + } + } diff --git a/module/spring-boot-devtools/src/intTest/java/org/springframework/boot/devtools/tests/DevToolsWithLazyInitializationIntegrationTests.java b/module/spring-boot-devtools/src/intTest/java/org/springframework/boot/devtools/tests/DevToolsWithLazyInitializationIntegrationTests.java index e536c6c4f4b..ae7200fc8f2 100644 --- a/module/spring-boot-devtools/src/intTest/java/org/springframework/boot/devtools/tests/DevToolsWithLazyInitializationIntegrationTests.java +++ b/module/spring-boot-devtools/src/intTest/java/org/springframework/boot/devtools/tests/DevToolsWithLazyInitializationIntegrationTests.java @@ -19,10 +19,7 @@ package org.springframework.boot.devtools.tests; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; -import org.springframework.boot.resttestclient.TestRestTemplate; -import org.springframework.http.HttpStatus; - -import static org.assertj.core.api.Assertions.assertThat; +import org.springframework.test.web.servlet.client.RestTestClient; /** * Integration tests for DevTools with lazy initialization enabled. @@ -36,15 +33,14 @@ class DevToolsWithLazyInitializationIntegrationTests extends AbstractDevToolsInt void addARequestMappingToAnExistingControllerWhenLazyInit(ApplicationLauncher applicationLauncher) throws Exception { launchApplication(applicationLauncher, "--spring.main.lazy-initialization=true"); - TestRestTemplate template = new TestRestTemplate(); + RestTestClient client = RestTestClient.bindToServer().build(); String urlBase = "http://localhost:" + awaitServerPort(); - assertThat(template.getForObject(urlBase + "/one", String.class)).isEqualTo("one"); - assertThat(template.getForEntity(urlBase + "/two", String.class).getStatusCode()) - .isEqualTo(HttpStatus.NOT_FOUND); + client.get().uri(urlBase + "/one").exchangeSuccessfully().expectBody(String.class).isEqualTo("one"); + client.get().uri(urlBase + "/two").exchange().expectStatus().isNotFound(); controller("com.example.ControllerOne").withRequestMapping("one").withRequestMapping("two").build(); urlBase = "http://localhost:" + awaitServerPort(); - assertThat(template.getForObject(urlBase + "/one", String.class)).isEqualTo("one"); - assertThat(template.getForObject(urlBase + "/two", String.class)).isEqualTo("two"); + client.get().uri(urlBase + "/one").exchangeSuccessfully().expectBody(String.class).isEqualTo("one"); + client.get().uri(urlBase + "/two").exchangeSuccessfully().expectBody(String.class).isEqualTo("two"); } static Object[] parameters() { diff --git a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomApplicationTests.java b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomApplicationTests.java index 0056e079e4f..4e38ef1c8ea 100644 --- a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomApplicationTests.java +++ b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomApplicationTests.java @@ -25,17 +25,18 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.resttestclient.TestRestTemplate; -import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.http.server.LocalTestWebServer; import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.web.client.RestClient; import static org.assertj.core.api.Assertions.assertThat; @@ -45,17 +46,17 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Stephane Nicoll */ @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -@AutoConfigureTestRestTemplate @DirtiesContext class JerseyAutoConfigurationCustomApplicationTests { @Autowired - private TestRestTemplate restTemplate; + private ApplicationContext applicationContext; @Test void contextLoads() { - ResponseEntity entity = this.restTemplate.getForEntity("/test/hello", String.class); - assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); + String uri = LocalTestWebServer.obtain(this.applicationContext).uri("/test/hello"); + ResponseEntity response = RestClient.create().get().uri(uri).retrieve().toEntity(String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); } @ApplicationPath("/test") diff --git a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomFilterContextPathTests.java b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomFilterContextPathTests.java index 980fbb7a681..564af5de496 100644 --- a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomFilterContextPathTests.java +++ b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomFilterContextPathTests.java @@ -32,16 +32,17 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.resttestclient.TestRestTemplate; -import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.http.server.LocalTestWebServer; import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.web.client.RestClient; import static org.assertj.core.api.Assertions.assertThat; @@ -53,17 +54,17 @@ import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "spring.jersey.type=filter", "server.servlet.context-path=/app", "server.servlet.register-default-servlet=true" }) -@AutoConfigureTestRestTemplate @DirtiesContext class JerseyAutoConfigurationCustomFilterContextPathTests { @Autowired - private TestRestTemplate restTemplate; + private ApplicationContext applicationContext; @Test void contextLoads() { - ResponseEntity entity = this.restTemplate.getForEntity("/rest/hello", String.class); - assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); + String uri = LocalTestWebServer.obtain(this.applicationContext).uri("/rest/hello"); + ResponseEntity response = RestClient.create().get().uri(uri).retrieve().toEntity(String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); } @MinimalWebConfiguration diff --git a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomFilterPathTests.java b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomFilterPathTests.java index 7c80d224b03..10befd6ab6d 100644 --- a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomFilterPathTests.java +++ b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomFilterPathTests.java @@ -32,16 +32,17 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.resttestclient.TestRestTemplate; -import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.http.server.LocalTestWebServer; import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.web.client.RestClient; import static org.assertj.core.api.Assertions.assertThat; @@ -52,17 +53,17 @@ import static org.assertj.core.api.Assertions.assertThat; */ @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "spring.jersey.type=filter", "server.servlet.register-default-servlet=true" }) -@AutoConfigureTestRestTemplate @DirtiesContext class JerseyAutoConfigurationCustomFilterPathTests { @Autowired - private TestRestTemplate restTemplate; + private ApplicationContext applicationContext; @Test void contextLoads() { - ResponseEntity entity = this.restTemplate.getForEntity("/rest/hello", String.class); - assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); + String uri = LocalTestWebServer.obtain(this.applicationContext).uri("/rest/hello"); + ResponseEntity response = RestClient.create().get().uri(uri).retrieve().toEntity(String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); } @MinimalWebConfiguration diff --git a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomObjectMapperProviderTests.java b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomObjectMapperProviderTests.java index d60816f68aa..00d0616bf2f 100644 --- a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomObjectMapperProviderTests.java +++ b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomObjectMapperProviderTests.java @@ -31,16 +31,17 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.resttestclient.TestRestTemplate; -import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.http.server.LocalTestWebServer; import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.web.client.RestClient; import static org.assertj.core.api.Assertions.assertThat; @@ -51,18 +52,18 @@ import static org.assertj.core.api.Assertions.assertThat; */ @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.jackson2.default-property-inclusion=non_null") -@AutoConfigureTestRestTemplate @DirtiesContext class JerseyAutoConfigurationCustomObjectMapperProviderTests { @Autowired - private TestRestTemplate restTemplate; + private ApplicationContext applicationContext; @Test void contextLoads() { - ResponseEntity response = this.restTemplate.getForEntity("/rest/message", String.class); + String uri = LocalTestWebServer.obtain(this.applicationContext).uri("/rest/message"); + ResponseEntity response = RestClient.create().get().uri(uri).retrieve().toEntity(String.class); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); - assertThat("{\"subject\":\"Jersey\"}").isEqualTo(response.getBody()); + assertThat(response.getBody()).isEqualTo("{\"subject\":\"Jersey\"}"); } @MinimalWebConfiguration diff --git a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomServletContextPathTests.java b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomServletContextPathTests.java index 7bd41efe1b1..b54c86f2f76 100644 --- a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomServletContextPathTests.java +++ b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomServletContextPathTests.java @@ -32,16 +32,17 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.resttestclient.TestRestTemplate; -import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.http.server.LocalTestWebServer; import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.web.client.RestClient; import static org.assertj.core.api.Assertions.assertThat; @@ -51,17 +52,17 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Dave Syer */ @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "server.servlet.contextPath=/app") -@AutoConfigureTestRestTemplate @DirtiesContext class JerseyAutoConfigurationCustomServletContextPathTests { @Autowired - private TestRestTemplate restTemplate; + private ApplicationContext applicationContext; @Test void contextLoads() { - ResponseEntity entity = this.restTemplate.getForEntity("/rest/hello", String.class); - assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); + String uri = LocalTestWebServer.obtain(this.applicationContext).uri("/rest/hello"); + ResponseEntity response = RestClient.create().get().uri(uri).retrieve().toEntity(String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); } @MinimalWebConfiguration diff --git a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomServletPathTests.java b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomServletPathTests.java index e402cbbc644..ca8859e28fc 100644 --- a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomServletPathTests.java +++ b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationCustomServletPathTests.java @@ -32,16 +32,17 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.resttestclient.TestRestTemplate; -import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.http.server.LocalTestWebServer; import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.web.client.RestClient; import static org.assertj.core.api.Assertions.assertThat; @@ -51,17 +52,17 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Dave Syer */ @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -@AutoConfigureTestRestTemplate @DirtiesContext class JerseyAutoConfigurationCustomServletPathTests { @Autowired - private TestRestTemplate restTemplate; + private ApplicationContext applicationContext; @Test void contextLoads() { - ResponseEntity entity = this.restTemplate.getForEntity("/rest/hello", String.class); - assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); + String uri = LocalTestWebServer.obtain(this.applicationContext).uri("/rest/hello"); + ResponseEntity response = RestClient.create().get().uri(uri).retrieve().toEntity(String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); } @MinimalWebConfiguration diff --git a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationDefaultFilterPathTests.java b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationDefaultFilterPathTests.java index 6e4523a9cd9..e9b2e401be7 100644 --- a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationDefaultFilterPathTests.java +++ b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationDefaultFilterPathTests.java @@ -31,16 +31,17 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.resttestclient.TestRestTemplate; -import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.http.server.LocalTestWebServer; import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.web.client.RestClient; import static org.assertj.core.api.Assertions.assertThat; @@ -51,17 +52,17 @@ import static org.assertj.core.api.Assertions.assertThat; */ @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "spring.jersey.type=filter", "server.servlet.register-default-servlet=true" }) -@AutoConfigureTestRestTemplate @DirtiesContext class JerseyAutoConfigurationDefaultFilterPathTests { @Autowired - private TestRestTemplate restTemplate; + private ApplicationContext applicationContext; @Test void contextLoads() { - ResponseEntity entity = this.restTemplate.getForEntity("/hello", String.class); - assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); + String uri = LocalTestWebServer.obtain(this.applicationContext).uri("/hello"); + ResponseEntity response = RestClient.create().get().uri(uri).retrieve().toEntity(String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); } @MinimalWebConfiguration diff --git a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationDefaultServletPathTests.java b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationDefaultServletPathTests.java index bbbf7445551..cdc7f302ae4 100644 --- a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationDefaultServletPathTests.java +++ b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationDefaultServletPathTests.java @@ -31,16 +31,17 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.resttestclient.TestRestTemplate; -import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.http.server.LocalTestWebServer; import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.web.client.RestClient; import static org.assertj.core.api.Assertions.assertThat; @@ -50,17 +51,17 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Dave Syer */ @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -@AutoConfigureTestRestTemplate @DirtiesContext class JerseyAutoConfigurationDefaultServletPathTests { @Autowired - private TestRestTemplate restTemplate; + private ApplicationContext applicationContext; @Test void contextLoads() { - ResponseEntity entity = this.restTemplate.getForEntity("/hello", String.class); - assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); + String uri = LocalTestWebServer.obtain(this.applicationContext).uri("/hello"); + ResponseEntity response = RestClient.create().get().uri(uri).retrieve().toEntity(String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); } @MinimalWebConfiguration diff --git a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationObjectMapperProviderTests.java b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationObjectMapperProviderTests.java index d6f5fa89281..55554f62506 100644 --- a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationObjectMapperProviderTests.java +++ b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationObjectMapperProviderTests.java @@ -32,16 +32,17 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.resttestclient.TestRestTemplate; -import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.http.server.LocalTestWebServer; import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.web.client.RestClient; import static org.assertj.core.api.Assertions.assertThat; @@ -53,16 +54,16 @@ import static org.assertj.core.api.Assertions.assertThat; */ @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.jackson2.default-property-inclusion:non-null") -@AutoConfigureTestRestTemplate @DirtiesContext class JerseyAutoConfigurationObjectMapperProviderTests { @Autowired - private TestRestTemplate restTemplate; + private ApplicationContext applicationContext; @Test void responseIsSerializedUsingAutoConfiguredObjectMapper() { - ResponseEntity response = this.restTemplate.getForEntity("/rest/message", String.class); + String uri = LocalTestWebServer.obtain(this.applicationContext).uri("/rest/message"); + ResponseEntity response = RestClient.create().get().uri(uri).retrieve().toEntity(String.class); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(response.getBody()).isEqualTo("{\"subject\":\"Jersey\"}"); } diff --git a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationWithoutApplicationPathTests.java b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationWithoutApplicationPathTests.java index 51d36468c23..b0adb20f3da 100644 --- a/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationWithoutApplicationPathTests.java +++ b/module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfigurationWithoutApplicationPathTests.java @@ -31,16 +31,17 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.resttestclient.TestRestTemplate; -import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.http.server.LocalTestWebServer; import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.web.client.RestClient; import static org.assertj.core.api.Assertions.assertThat; @@ -50,17 +51,17 @@ import static org.assertj.core.api.Assertions.assertThat; * @author EddĂș MelĂ©ndez */ @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.jersey.application-path=/api") -@AutoConfigureTestRestTemplate @DirtiesContext class JerseyAutoConfigurationWithoutApplicationPathTests { @Autowired - private TestRestTemplate restTemplate; + private ApplicationContext applicationContext; @Test void contextLoads() { - ResponseEntity entity = this.restTemplate.getForEntity("/api/hello", String.class); - assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); + String uri = LocalTestWebServer.obtain(this.applicationContext).uri("/api/hello"); + ResponseEntity response = RestClient.create().get().uri(uri).retrieve().toEntity(String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); } @MinimalWebConfiguration diff --git a/module/spring-boot-mustache/src/test/java/org/springframework/boot/mustache/autoconfigure/MustacheAutoConfigurationServletIntegrationTests.java b/module/spring-boot-mustache/src/test/java/org/springframework/boot/mustache/autoconfigure/MustacheAutoConfigurationServletIntegrationTests.java index d0f8528d2d9..409a2cf19ea 100644 --- a/module/spring-boot-mustache/src/test/java/org/springframework/boot/mustache/autoconfigure/MustacheAutoConfigurationServletIntegrationTests.java +++ b/module/spring-boot-mustache/src/test/java/org/springframework/boot/mustache/autoconfigure/MustacheAutoConfigurationServletIntegrationTests.java @@ -22,24 +22,22 @@ import java.util.Map; import com.samskivert.mustache.Mustache; import com.samskivert.mustache.Template; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.mustache.servlet.view.MustacheView; import org.springframework.boot.mustache.servlet.view.MustacheViewResolver; -import org.springframework.boot.resttestclient.TestRestTemplate; +import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; -import org.springframework.boot.web.server.WebServer; -import org.springframework.boot.web.server.servlet.context.ServletWebServerApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.stereotype.Controller; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.web.servlet.client.RestTestClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.DispatcherServlet; @@ -54,19 +52,11 @@ import static org.assertj.core.api.Assertions.assertThat; */ @DirtiesContext @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@AutoConfigureRestTestClient class MustacheAutoConfigurationServletIntegrationTests { @Autowired - private ServletWebServerApplicationContext context; - - private int port; - - @BeforeEach - void init() { - WebServer webServer = this.context.getWebServer(); - assertThat(webServer).isNotNull(); - this.port = webServer.getPort(); - } + private RestTestClient restTestClient; @Test void shouldRenderTemplate() { @@ -79,14 +69,20 @@ class MustacheAutoConfigurationServletIntegrationTests { @Test void testHomePage() { - String body = new TestRestTemplate().getForObject("http://localhost:" + this.port, String.class); - assertThat(body).contains("Hello World"); + this.restTestClient.get() + .uri("/") + .exchangeSuccessfully() + .expectBody(String.class) + .value((body) -> assertThat(body).contains("Hello World")); } @Test void testPartialPage() { - String body = new TestRestTemplate().getForObject("http://localhost:" + this.port + "/partial", String.class); - assertThat(body).contains("Hello World"); + this.restTestClient.get() + .uri("/partial") + .exchangeSuccessfully() + .expectBody(String.class) + .value((body) -> assertThat(body).contains("Hello World")); } @Configuration(proxyBeanMethods = false) diff --git a/module/spring-boot-security/src/test/java/org/springframework/boot/security/autoconfigure/web/servlet/SecurityFilterAutoConfigurationEarlyInitializationTests.java b/module/spring-boot-security/src/test/java/org/springframework/boot/security/autoconfigure/web/servlet/SecurityFilterAutoConfigurationEarlyInitializationTests.java index f3e1d0e6970..c3c24bebb6c 100644 --- a/module/spring-boot-security/src/test/java/org/springframework/boot/security/autoconfigure/web/servlet/SecurityFilterAutoConfigurationEarlyInitializationTests.java +++ b/module/spring-boot-security/src/test/java/org/springframework/boot/security/autoconfigure/web/servlet/SecurityFilterAutoConfigurationEarlyInitializationTests.java @@ -32,7 +32,6 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.http.converter.autoconfigure.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.jackson.autoconfigure.JacksonAutoConfiguration; -import org.springframework.boot.resttestclient.TestRestTemplate; import org.springframework.boot.security.autoconfigure.SecurityAutoConfiguration; import org.springframework.boot.security.autoconfigure.UserDetailsServiceAutoConfiguration; import org.springframework.boot.test.system.CapturedOutput; @@ -51,6 +50,7 @@ import org.springframework.context.annotation.Import; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Component; +import org.springframework.test.web.servlet.client.RestTestClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -82,7 +82,12 @@ class SecurityFilterAutoConfigurationEarlyInitializationTests { int port = webServer.getPort(); Matcher password = PASSWORD_PATTERN.matcher(output); assertThat(password.find()).isTrue(); - new TestRestTemplate("user", password.group(1)).getForEntity("http://localhost:" + port, Object.class); + RestTestClient.bindToServer() + .defaultHeaders((headers) -> headers.setBasicAuth("user", password.group(1))) + .build() + .get() + .uri("http://localhost:" + port) + .exchange(); // If early initialization occurred a ConverterNotFoundException is thrown } } diff --git a/module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/WelcomePageIntegrationTests.java b/module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/WelcomePageIntegrationTests.java index 08333d30b6d..c702de2071a 100644 --- a/module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/WelcomePageIntegrationTests.java +++ b/module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/WelcomePageIntegrationTests.java @@ -16,14 +16,11 @@ package org.springframework.boot.webmvc.autoconfigure; -import java.net.URI; - import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.http.converter.autoconfigure.HttpMessageConvertersAutoConfiguration; -import org.springframework.boot.resttestclient.TestRestTemplate; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.testsupport.classpath.resources.WithResource; import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; @@ -32,8 +29,7 @@ import org.springframework.boot.web.server.context.WebServerApplicationContext; import org.springframework.boot.web.server.servlet.context.AnnotationConfigServletWebServerApplicationContext; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; -import org.springframework.http.RequestEntity; -import org.springframework.http.ResponseEntity; +import org.springframework.test.web.servlet.client.RestTestClient; import static org.assertj.core.api.Assertions.assertThat; @@ -53,7 +49,7 @@ class WelcomePageIntegrationTests { WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, TomcatServletWebServerAutoConfiguration.class, DispatcherServletAutoConfiguration.class)); - private final TestRestTemplate template = new TestRestTemplate(); + private final RestTestClient client = RestTestClient.bindToServer().build(); @Test void contentStrategyWithWelcomePage() { @@ -61,12 +57,12 @@ class WelcomePageIntegrationTests { WebServer webServer = ((WebServerApplicationContext) context.getSourceApplicationContext()).getWebServer(); assertThat(webServer).isNotNull(); int port = webServer.getPort(); - RequestEntity entity = RequestEntity.get(new URI("http://localhost:" + port + "/")) + this.client.get() + .uri("http://localhost:" + port + "/") .header("Accept", MediaType.ALL.toString()) - .build(); - ResponseEntity content = this.template.exchange(entity, String.class); - assertThat(content.getBody()).contains("custom welcome page"); - assertThat(content.getStatusCode()).isEqualTo(HttpStatus.OK); + .exchangeSuccessfully() + .expectBody(String.class) + .value((body) -> assertThat(body).contains("custom welcome page")); }); } @@ -76,11 +72,12 @@ class WelcomePageIntegrationTests { WebServer webServer = ((WebServerApplicationContext) context.getSourceApplicationContext()).getWebServer(); assertThat(webServer).isNotNull(); int port = webServer.getPort(); - RequestEntity entity = RequestEntity.get(new URI("http://localhost:" + port + "/")) + this.client.get() + .uri("http://localhost:" + port + "/") .header("Accept", "spring/boot") - .build(); - ResponseEntity content = this.template.exchange(entity, String.class); - assertThat(content.getStatusCode()).isEqualTo(HttpStatus.NOT_ACCEPTABLE); + .exchange() + .expectStatus() + .isEqualTo(HttpStatus.NOT_ACCEPTABLE); }); } diff --git a/module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/error/BasicErrorControllerIntegrationTests.java b/module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/error/BasicErrorControllerIntegrationTests.java index 404162d6e21..a87dfa38e4c 100755 --- a/module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/error/BasicErrorControllerIntegrationTests.java +++ b/module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/error/BasicErrorControllerIntegrationTests.java @@ -23,7 +23,6 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Method; import java.lang.reflect.Parameter; -import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -43,7 +42,6 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebProperties; import org.springframework.boot.http.converter.autoconfigure.HttpMessageConvertersAutoConfiguration; -import org.springframework.boot.resttestclient.TestRestTemplate; import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; import org.springframework.boot.web.error.ErrorAttributeOptions; import org.springframework.boot.web.error.ErrorAttributeOptions.Include; @@ -56,8 +54,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.MethodParameter; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; -import org.springframework.http.RequestEntity; -import org.springframework.http.ResponseEntity; +import org.springframework.test.web.servlet.client.RestTestClient; +import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; import org.springframework.validation.BindException; @@ -93,13 +91,11 @@ class BasicErrorControllerIntegrationTests { } @Test - @SuppressWarnings({ "rawtypes", "unchecked" }) void testErrorForMachineClientDefault() { load(); - ResponseEntity entity = new TestRestTemplate().getForEntity(createUrl("?trace=true"), Map.class); - assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", null, null, "/"); - assertThat(entity.getBody()).doesNotContainKey("exception"); - assertThat(entity.getBody()).doesNotContainKey("trace"); + Map response = getResponseMap("?trace=true"); + assertErrorAttributes(response, "500", "Internal Server Error", null, null, "/"); + assertThat(response).doesNotContainKey("exception").doesNotContainKey("trace"); } @Test @@ -138,44 +134,39 @@ class BasicErrorControllerIntegrationTests { } @Test - @SuppressWarnings("rawtypes") void testErrorForMachineClientAlwaysParamsWithoutMessage() { load("--spring.web.error.include-exception=true", "--spring.web.error.include-message=always"); - ResponseEntity entity = new TestRestTemplate().getForEntity(createUrl("/noMessage"), Map.class); - assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", IllegalStateException.class, + Map response = getResponseMap("/noMessage"); + assertErrorAttributes(response, "500", "Internal Server Error", IllegalStateException.class, "No message available", "/noMessage"); } - @SuppressWarnings({ "rawtypes", "unchecked" }) private void exceptionWithStackTraceAndMessage(String path) { - ResponseEntity entity = new TestRestTemplate().getForEntity(createUrl(path), Map.class); - assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", IllegalStateException.class, - "Expected!", "/"); - assertThat(entity.getBody()).containsKey("trace"); + Map response = getResponseMap(path); + assertErrorAttributes(response, "500", "Internal Server Error", IllegalStateException.class, "Expected!", "/"); + assertThat(response).containsKey("trace"); } - @SuppressWarnings({ "rawtypes", "unchecked" }) private void exceptionWithoutStackTraceAndMessage(String path) { - ResponseEntity entity = new TestRestTemplate().getForEntity(createUrl(path), Map.class); - assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", IllegalStateException.class, null, "/"); - assertThat(entity.getBody()).doesNotContainKey("trace"); + Map response = getResponseMap(path); + assertErrorAttributes(response, "500", "Internal Server Error", IllegalStateException.class, null, "/"); + assertThat(response).doesNotContainKey("trace"); } @Test - @SuppressWarnings("rawtypes") void testErrorForAnnotatedExceptionWithoutMessage() { load("--spring.web.error.include-exception=true"); - ResponseEntity entity = new TestRestTemplate().getForEntity(createUrl("/annotated"), Map.class); - assertErrorAttributes(entity.getBody(), "400", "Bad Request", TestConfiguration.Errors.ExpectedException.class, - null, "/annotated"); + Map response = getResponseMap("/annotated"); + assertErrorAttributes(response, "400", "Bad Request", TestConfiguration.Errors.ExpectedException.class, null, + "/annotated"); } @Test @SuppressWarnings("rawtypes") void testErrorForAnnotatedExceptionWithMessage() { load("--spring.web.error.include-exception=true", "--spring.web.error.include-message=always"); - ResponseEntity entity = new TestRestTemplate().getForEntity(createUrl("/annotated"), Map.class); - assertErrorAttributes(entity.getBody(), "400", "Bad Request", TestConfiguration.Errors.ExpectedException.class, + Map response = getResponseMap("/annotated"); + assertErrorAttributes(response, "400", "Bad Request", TestConfiguration.Errors.ExpectedException.class, "Expected!", "/annotated"); } @@ -183,8 +174,8 @@ class BasicErrorControllerIntegrationTests { @SuppressWarnings("rawtypes") void testErrorForAnnotatedNoReasonExceptionWithoutMessage() { load("--spring.web.error.include-exception=true"); - ResponseEntity entity = new TestRestTemplate().getForEntity(createUrl("/annotatedNoReason"), Map.class); - assertErrorAttributes(entity.getBody(), "406", "Not Acceptable", + Map response = getResponseMap("/annotatedNoReason"); + assertErrorAttributes(response, "406", "Not Acceptable", TestConfiguration.Errors.NoReasonExpectedException.class, null, "/annotatedNoReason"); } @@ -192,8 +183,8 @@ class BasicErrorControllerIntegrationTests { @SuppressWarnings("rawtypes") void testErrorForAnnotatedNoReasonExceptionWithMessage() { load("--spring.web.error.include-exception=true", "--spring.web.error.include-message=always"); - ResponseEntity entity = new TestRestTemplate().getForEntity(createUrl("/annotatedNoReason"), Map.class); - assertErrorAttributes(entity.getBody(), "406", "Not Acceptable", + Map response = getResponseMap("/annotatedNoReason"); + assertErrorAttributes(response, "406", "Not Acceptable", TestConfiguration.Errors.NoReasonExpectedException.class, "Expected message", "/annotatedNoReason"); } @@ -201,8 +192,8 @@ class BasicErrorControllerIntegrationTests { @SuppressWarnings("rawtypes") void testErrorForAnnotatedNoMessageExceptionWithMessage() { load("--spring.web.error.include-exception=true", "--spring.web.error.include-message=always"); - ResponseEntity entity = new TestRestTemplate().getForEntity(createUrl("/annotatedNoMessage"), Map.class); - assertErrorAttributes(entity.getBody(), "406", "Not Acceptable", + Map response = getResponseMap("/annotatedNoMessage"); + assertErrorAttributes(response, "406", "Not Acceptable", TestConfiguration.Errors.NoReasonExpectedException.class, "No message available", "/annotatedNoMessage"); } @@ -267,87 +258,94 @@ class BasicErrorControllerIntegrationTests { bindingExceptionWithoutMessage("?message=true"); } - @SuppressWarnings({ "rawtypes", "unchecked" }) private void bindingExceptionWithErrors(String param) { - ResponseEntity entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class); - assertErrorAttributes(entity.getBody(), "400", "Bad Request", MethodArgumentNotValidException.class, null, - "/bind"); - assertThat(entity.getBody()).containsKey("errors"); + Map response = getResponseMap("/bind" + param); + assertErrorAttributes(response, "400", "Bad Request", MethodArgumentNotValidException.class, null, "/bind"); + assertThat(response).containsKey("errors"); } - @SuppressWarnings({ "rawtypes", "unchecked" }) private void bindingExceptionWithoutErrors(String param) { - ResponseEntity entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class); - assertErrorAttributes(entity.getBody(), "400", "Bad Request", MethodArgumentNotValidException.class, null, - "/bind"); - assertThat(entity.getBody()).doesNotContainKey("errors"); + Map response = getResponseMap("/bind" + param); + assertErrorAttributes(response, "400", "Bad Request", MethodArgumentNotValidException.class, null, "/bind"); + assertThat(response).doesNotContainKey("errors"); } - @SuppressWarnings({ "rawtypes", "unchecked" }) private void bindingExceptionWithMessage(String param) { - ResponseEntity entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class); - assertErrorAttributes(entity.getBody(), "400", "Bad Request", MethodArgumentNotValidException.class, + Map response = getResponseMap("/bind" + param); + assertErrorAttributes(response, "400", "Bad Request", MethodArgumentNotValidException.class, "Validation failed for object='test'. Error count: 1", "/bind"); - assertThat(entity.getBody()).doesNotContainKey("errors"); + assertThat(response).doesNotContainKey("errors"); } - @SuppressWarnings({ "rawtypes", "unchecked" }) private void bindingExceptionWithoutMessage(String param) { - ResponseEntity entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class); - assertErrorAttributes(entity.getBody(), "400", "Bad Request", MethodArgumentNotValidException.class, null, - "/bind"); - assertThat(entity.getBody()).doesNotContainKey("errors"); + Map response = getResponseMap("/bind" + param); + assertErrorAttributes(response, "400", "Bad Request", MethodArgumentNotValidException.class, null, "/bind"); + assertThat(response).doesNotContainKey("errors"); } @Test - @SuppressWarnings({ "rawtypes", "unchecked" }) + @SuppressWarnings({ "unchecked" }) void testRequestBodyValidationForMachineClient() { load("--spring.web.error.include-exception=true"); - RequestEntity request = RequestEntity.post(URI.create(createUrl("/bodyValidation"))) - .accept(MediaType.APPLICATION_JSON) + Map response = createRestClient().post() + .uri("/bodyValidation") .contentType(MediaType.APPLICATION_JSON) - .body("{}"); - ResponseEntity entity = new TestRestTemplate().exchange(request, Map.class); - assertErrorAttributes(entity.getBody(), "400", "Bad Request", MethodArgumentNotValidException.class, null, + .accept(MediaType.APPLICATION_JSON) + .body("{}") + .exchange() + .expectBody(Map.class) + .returnResult() + .getResponseBody(); + assertErrorAttributes(response, "400", "Bad Request", MethodArgumentNotValidException.class, null, "/bodyValidation"); - assertThat(entity.getBody()).doesNotContainKey("errors"); + assertThat(response).doesNotContainKey("errors"); } @Test @SuppressWarnings({ "rawtypes", "unchecked" }) void testBindingExceptionForMachineClientDefault() { load(); - RequestEntity request = RequestEntity.get(URI.create(createUrl("/bind?trace=true,message=true"))) + Map response = createRestClient().get() + .uri("/bind?trace=true,message=true") .accept(MediaType.APPLICATION_JSON) - .build(); - ResponseEntity entity = new TestRestTemplate().exchange(request, Map.class); - assertThat(entity.getBody()).doesNotContainKey("exception"); - assertThat(entity.getBody()).doesNotContainKey("trace"); - assertThat(entity.getBody()).doesNotContainKey("errors"); + .exchange() + .expectBody(Map.class) + .returnResult() + .getResponseBody(); + assertThat(response).doesNotContainKey("exception"); + assertThat(response).doesNotContainKey("trace"); + assertThat(response).doesNotContainKey("errors"); } @Test void testIncompatibleMediaType() { load(); - RequestEntity request = RequestEntity.get(URI.create(createUrl("/incompatibleType"))) + createRestClient().get() + .uri("/incompatibleType") .accept(MediaType.TEXT_PLAIN) - .build(); - ResponseEntity entity = new TestRestTemplate().exchange(request, String.class); - assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); - assertThat(entity.getHeaders().getContentType()).isNull(); - assertThat(entity.getBody()).isNull(); + .exchange() + .expectStatus() + .isBadRequest() + .expectHeader() + .doesNotExist("Content-Type") + .expectBody() + .isEmpty(); } @Test @SuppressWarnings({ "rawtypes", "unchecked" }) void customErrorControllerWithoutStatusConfiguration() { load(CustomErrorControllerWithoutStatusConfiguration.class); - RequestEntity request = RequestEntity.post(URI.create(createUrl("/bodyValidation"))) + Map response = createRestClient().post() + .uri("/bodyValidation") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON) - .body("{}"); - ResponseEntity entity = new TestRestTemplate().exchange(request, Map.class); - assertThat(entity.getBody()).doesNotContainKey("status"); + .body("{}") + .exchange() + .expectBody(Map.class) + .returnResult() + .getResponseBody(); + assertThat(response).doesNotContainKey("status"); } private void assertErrorAttributes(@Nullable Map content, String status, String error, @@ -365,11 +363,23 @@ class BasicErrorControllerIntegrationTests { assertThat(content.get("path")).as("Wrong path").isEqualTo(path); } - private String createUrl(String path) { + @SuppressWarnings("unchecked") + private Map getResponseMap(String url) { + Map responseBody = createRestClient().get() + .uri(url) + .exchange() + .expectBody(Map.class) + .returnResult() + .getResponseBody(); + Assert.notNull(responseBody, "Response body should not be null"); + return responseBody; + } + + RestTestClient createRestClient() { assertThat(this.context).isNotNull(); Integer port = this.context.getEnvironment().getProperty("local.server.port", Integer.class); assertThat(port).isNotNull(); - return "http://localhost:" + port + path; + return RestTestClient.bindToServer().baseUrl("http://localhost:" + port).build(); } private void load(String... arguments) { diff --git a/module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/error/RemappedErrorViewIntegrationTests.java b/module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/error/RemappedErrorViewIntegrationTests.java index 006e87cfcc5..5b97d97445e 100644 --- a/module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/error/RemappedErrorViewIntegrationTests.java +++ b/module/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/error/RemappedErrorViewIntegrationTests.java @@ -18,13 +18,13 @@ package org.springframework.boot.webmvc.autoconfigure.error; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.http.converter.autoconfigure.HttpMessageConvertersAutoConfiguration; -import org.springframework.boot.resttestclient.TestRestTemplate; +import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; import org.springframework.boot.web.error.ErrorPage; import org.springframework.boot.web.error.ErrorPageRegistrar; @@ -35,6 +35,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.stereotype.Controller; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.web.servlet.client.RestTestClient; import org.springframework.web.bind.annotation.RequestMapping; import static org.assertj.core.api.Assertions.assertThat; @@ -45,26 +46,29 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Dave Syer */ @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.mvc.servlet.path:/spring/") +@AutoConfigureRestTestClient @DirtiesContext class RemappedErrorViewIntegrationTests { - @LocalServerPort - private int port; - - private final TestRestTemplate template = new TestRestTemplate(); + @Autowired + private RestTestClient client; @Test void directAccessToErrorPage() { - String content = this.template.getForObject("http://localhost:" + this.port + "/spring/error", String.class); - assertThat(content).contains("error"); - assertThat(content).contains("999"); + this.client.get() + .uri("/spring/error") + .exchange() + .expectBody(String.class) + .value((content) -> assertThat(content).contains("error").contains("999")); } @Test void forwardToErrorPage() { - String content = this.template.getForObject("http://localhost:" + this.port + "/spring/", String.class); - assertThat(content).contains("error"); - assertThat(content).contains("500"); + this.client.get() + .uri("/spring/") + .exchange() + .expectBody(String.class) + .value((content) -> assertThat(content).contains("error").contains("500")); } @Configuration(proxyBeanMethods = false)