From d6dc888e4a9a7664689dd3e453522f9447151db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Fri, 28 Nov 2025 15:57:10 +0100 Subject: [PATCH] Focus examples on TestRestTemplate configuration Closes gh-48334 --- .../utilities/testresttemplate/MySpringBootTests.java | 8 +++++--- .../docs/testing/utilities/testresttemplate/MyTests.java | 7 +++++-- .../utilities/testresttemplate/MySpringBootTests.kt | 6 ++++-- .../docs/testing/utilities/testresttemplate/MyTests.kt | 6 ++++-- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/utilities/testresttemplate/MySpringBootTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/utilities/testresttemplate/MySpringBootTests.java index 9d7304022e8..28561262e73 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/utilities/testresttemplate/MySpringBootTests.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/utilities/testresttemplate/MySpringBootTests.java @@ -27,7 +27,8 @@ import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; -import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import static org.assertj.core.api.Assertions.assertThat; @@ -39,8 +40,9 @@ class MySpringBootTests { @Test void testRequest() { - HttpHeaders headers = this.template.getForEntity("/example", String.class).getHeaders(); - assertThat(headers.getLocation()).hasHost("other.example.com"); + ResponseEntity response = this.template.getForEntity("/example", String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + // Other assertions to verify the response } @TestConfiguration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/utilities/testresttemplate/MyTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/utilities/testresttemplate/MyTests.java index e4106823ac2..d659288c8b9 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/utilities/testresttemplate/MyTests.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/utilities/testresttemplate/MyTests.java @@ -19,6 +19,7 @@ package org.springframework.boot.docs.testing.utilities.testresttemplate; import org.junit.jupiter.api.Test; import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import static org.assertj.core.api.Assertions.assertThat; @@ -29,8 +30,10 @@ class MyTests { @Test void testRequest() { - ResponseEntity headers = this.template.getForEntity("https://myhost.example.com/example", String.class); - assertThat(headers.getHeaders().getLocation()).hasHost("other.example.com"); + ResponseEntity response = this.template.getForEntity("https://myhost.example.com/example", + String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + // Other assertions to verify the response } } diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/utilities/testresttemplate/MySpringBootTests.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/utilities/testresttemplate/MySpringBootTests.kt index 77b120c143e..1be64c2a31c 100644 --- a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/utilities/testresttemplate/MySpringBootTests.kt +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/utilities/testresttemplate/MySpringBootTests.kt @@ -25,6 +25,7 @@ import org.springframework.boot.test.context.TestConfiguration import org.springframework.boot.test.web.client.TestRestTemplate import org.springframework.boot.web.client.RestTemplateBuilder import org.springframework.context.annotation.Bean +import org.springframework.http.HttpStatus import java.time.Duration @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @@ -32,8 +33,9 @@ class MySpringBootTests(@Autowired val template: TestRestTemplate) { @Test fun testRequest() { - val headers = template.getForEntity("/example", String::class.java).headers - assertThat(headers.location).hasHost("other.example.com") + val response = template.getForEntity("/example", String::class.java) + assertThat(response.statusCode).isEqualTo(HttpStatus.OK) + // Other assertions to verify the response } @TestConfiguration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/utilities/testresttemplate/MyTests.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/utilities/testresttemplate/MyTests.kt index f3ce2e1e0ee..885f8520b10 100644 --- a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/utilities/testresttemplate/MyTests.kt +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/utilities/testresttemplate/MyTests.kt @@ -19,6 +19,7 @@ package org.springframework.boot.docs.testing.utilities.testresttemplate import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.springframework.boot.test.web.client.TestRestTemplate +import org.springframework.http.HttpStatus class MyTests { @@ -26,8 +27,9 @@ class MyTests { @Test fun testRequest() { - val headers = template.getForEntity("https://myhost.example.com/example", String::class.java) - assertThat(headers.headers.location).hasHost("other.example.com") + val response = template.getForEntity("https://myhost.example.com/example", String::class.java) + assertThat(response.statusCode).isEqualTo(HttpStatus.OK) + // Other assertions to verify the response } }