mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-10-10 19:39:01 +00:00
Focus examples on TestRestTemplate configuration
Closes gh-48334
This commit is contained in:
+5
-3
@@ -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<String> response = this.template.getForEntity("/example", String.class);
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
// Other assertions to verify the response
|
||||
}
|
||||
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
|
||||
+5
-2
@@ -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<String> headers = this.template.getForEntity("https://myhost.example.com/example", String.class);
|
||||
assertThat(headers.getHeaders().getLocation()).hasHost("other.example.com");
|
||||
ResponseEntity<String> response = this.template.getForEntity("https://myhost.example.com/example",
|
||||
String.class);
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
// Other assertions to verify the response
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+4
-2
@@ -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)
|
||||
|
||||
+4
-2
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user