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
This commit is contained in:
parent
0660f52abb
commit
753a7e1d33
|
@ -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]]
|
||||
|
|
|
@ -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.</li>
|
||||
* <li>Registers a {@link org.springframework.boot.test.web.client.TestRestTemplate
|
||||
* TestRestTemplate} bean for use in web tests.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Phillip Webb
|
||||
|
|
|
@ -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()}.
|
||||
* <p>
|
||||
* 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
|
||||
|
|
Loading…
Reference in New Issue