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. + *
+ * 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