Skip to content

Commit 14ff776

Browse files
committed
Fix TestRestTemplate example in the doc
Closes gh-12132
1 parent 98f58ca commit 14ff776

File tree

5 files changed

+134
-28
lines changed

5 files changed

+134
-28
lines changed

spring-boot-docs/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,11 @@
742742
<artifactId>spring-boot-test-support</artifactId>
743743
<scope>test</scope>
744744
</dependency>
745+
<dependency>
746+
<groupId>org.springframework.boot</groupId>
747+
<artifactId>spring-boot-starter-web</artifactId>
748+
<scope>test</scope>
749+
</dependency>
745750
</dependencies>
746751
<build>
747752
<plugins>

spring-boot-docs/src/main/asciidoc/index.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Phillip Webb; Dave Syer; Josh Long; Stéphane Nicoll; Rob Winch; Andy Wilkinson;
5050
:propdeps-plugin: https://github.com/spring-projects/gradle-plugins/tree/master/propdeps-plugin
5151
:ant-manual: http://ant.apache.org/manual
5252
:code-examples: ../java/org/springframework/boot
53+
:test-examples: ../../test/java/org/springframework/boot
5354
:gradle-user-guide: https://docs.gradle.org/2.14.1/userguide
5455
:gradle-dsl: https://docs.gradle.org/2.14.1/dsl
5556
// ======================================================================================

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6207,8 +6207,9 @@ features will be enabled:
62076207
62086208
@Test
62096209
public void testRequest() throws Exception {
6210-
HttpHeaders headers = template.getForEntity("http://myhost.com/example", String.class).getHeaders();
6211-
assertThat(headers.getLocation().toString(), containsString("myotherhost"));
6210+
HttpHeaders headers = this.template.getForEntity(
6211+
"http://myhost.example.com/example", String.class).getHeaders();
6212+
assertThat(headers.getLocation()).hasHost("other.example.com");
62126213
}
62136214
62146215
}
@@ -6222,32 +6223,7 @@ specify a host and port will automatically connect to the embedded server:
62226223

62236224
[source,java,indent=0]
62246225
----
6225-
@RunWith(SpringRunner.class)
6226-
@SpringBootTest
6227-
public class MyTest {
6228-
6229-
@Autowired
6230-
private TestRestTemplate template;
6231-
6232-
@Test
6233-
public void testRequest() throws Exception {
6234-
HttpHeaders headers = template.getForEntity("/example", String.class).getHeaders();
6235-
assertThat(headers.getLocation().toString(), containsString("myotherhost"));
6236-
}
6237-
6238-
@TestConfiguration
6239-
static class Config {
6240-
6241-
@Bean
6242-
public RestTemplateBuilder restTemplateBuilder() {
6243-
return new RestTemplateBuilder()
6244-
.additionalMessageConverters(...)
6245-
.customizers(...);
6246-
}
6247-
6248-
}
6249-
6250-
}
6226+
include::{test-examples}/web/client/SampleWebClientTests.java[tag=test]
62516227
----
62526228

62536229

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.web.client;
18+
19+
import java.net.URI;
20+
21+
import org.springframework.boot.SpringBootConfiguration;
22+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
23+
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
24+
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
25+
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
27+
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
28+
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
29+
import org.springframework.http.ResponseEntity;
30+
import org.springframework.web.bind.annotation.RequestMapping;
31+
import org.springframework.web.bind.annotation.RestController;
32+
33+
/**
34+
* A sample {@link SpringBootConfiguration} with an example controller.
35+
*
36+
* @author Stephane Nicoll
37+
*/
38+
@SpringBootConfiguration
39+
@ImportAutoConfiguration({ EmbeddedServletContainerAutoConfiguration.class,
40+
ServerPropertiesAutoConfiguration.class,
41+
DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class,
42+
JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class })
43+
class SampleWebClientConfiguration {
44+
45+
46+
@RestController
47+
private static class ExampleController {
48+
49+
@RequestMapping("/example")
50+
public ResponseEntity<String> example() {
51+
return ResponseEntity.ok()
52+
.location(URI.create("https://other.example.com/example"))
53+
.body("test");
54+
}
55+
56+
}
57+
58+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.web.client;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.context.SpringBootTest;
24+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
25+
import org.springframework.boot.test.context.TestConfiguration;
26+
import org.springframework.boot.test.web.client.TestRestTemplate;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.http.HttpHeaders;
29+
import org.springframework.test.context.junit4.SpringRunner;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Example integration test that uses {@link TestRestTemplate}.
35+
*
36+
* @author Stephane Nicoll
37+
*/
38+
// tag::test[]
39+
@RunWith(SpringRunner.class)
40+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
41+
public class SampleWebClientTests {
42+
43+
@Autowired
44+
private TestRestTemplate template;
45+
46+
@Test
47+
public void testRequest() {
48+
HttpHeaders headers = this.template.getForEntity("/example", String.class)
49+
.getHeaders();
50+
assertThat(headers.getLocation()).hasHost("other.example.com");
51+
}
52+
53+
@TestConfiguration
54+
static class Config {
55+
56+
@Bean
57+
public RestTemplateBuilder restTemplateBuilder() {
58+
return new RestTemplateBuilder()
59+
.setConnectTimeout(1000)
60+
.setReadTimeout(1000);
61+
}
62+
63+
}
64+
65+
}
66+
// end::test[]

0 commit comments

Comments
 (0)