From 753a7e1d33140af6828546356c02298eabb59f1e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 26 Jul 2016 22:08:53 -0700 Subject: [PATCH] Document how to to customize the TestRestTemplate Update the reference documentation and add some additional Javadoc to provide hints on how to customize the `TestRestTemplate`. Fixes gh-6465 --- .../main/asciidoc/spring-boot-features.adoc | 34 +++++++++++++++++++ .../boot/test/context/SpringBootTest.java | 2 ++ .../test/web/client/TestRestTemplate.java | 7 ++++ 3 files changed, 43 insertions(+) diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index acc24aa6e09..afa5ec9a852 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -5468,6 +5468,40 @@ public class MyTest { } ---- +If you are using the `@SpringBootTest` annotation, you can just inject a fully configured +`TestRestTemplate` and start usinging it. If necessary, additional customizations can be +applied via the `RestTemplateBuilder` bean: + +[source,java,indent=0] +---- +@RunWith(SpringRunner.class) +@SpringBootTest +public class MyTest { + + @Autowired + private TestRestTemplate template; + + @Test + public void testRequest() throws Exception { + HttpHeaders headers = template.getForEntity("http://myhost.com", String.class).getHeaders(); + assertThat(headers.getLocation().toString(), containsString("myotherhost")); + } + + @TestConfig + static class Config { + + @Bean + public RestTemplateBuilder restTemplateBuilder() { + return new RestTemplateBuilder() + .additionalMessageConverters(...) + .customizers(...); + } + + } + +} +---- + [[boot-features-websockets]] diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTest.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTest.java index 9686c98fb38..2ec896c9ef3 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTest.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTest.java @@ -54,6 +54,8 @@ import org.springframework.web.context.WebApplicationContext; * including the ability to start a fully running container listening on a * {@link WebEnvironment#DEFINED_PORT defined} or {@link WebEnvironment#RANDOM_PORT * random} port. + *
  • Registers a {@link org.springframework.boot.test.web.client.TestRestTemplate + * TestRestTemplate} bean for use in web tests.
  • * * * @author Phillip Webb diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java b/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java index 7916e8f609e..2ff38c93cad 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java @@ -36,6 +36,7 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.protocol.HttpContext; import org.apache.http.ssl.SSLContextBuilder; +import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -66,6 +67,12 @@ import org.springframework.web.util.UriTemplateHandler; * Note: To prevent injection problems this class internally does not extend * {@link RestTemplate}. If you need access to the underlying {@link RestTemplate} use * {@link #getRestTemplate()}. + *

    + * If you are using the + * {@link org.springframework.boot.test.context.SpringBootTest @SpringBootTest} + * annotation, a {@link TestRestTemplate} is automatically available and can be + * {@code @Autowired} into you test. If you need customizations (for example to adding + * additional message converters) use a {@link RestTemplateBuilder} {@code @Bean}. * * @author Dave Syer * @author Phillip Webb