@@ -6047,11 +6047,16 @@ public class MyTest {
6047
6047
`TestRestTemplate` is a convenience alternative to Spring's `RestTemplate` that is useful
6048
6048
in integration tests. You can get a vanilla template or one that sends Basic HTTP
6049
6049
authentication (with a username and password). In either case the template will behave
6050
- in a test-friendly way: not following redirects (so you can assert the response location),
6051
- ignoring cookies (so the template is stateless), and not throwing exceptions on
6052
- server-side errors. It is recommended, but not mandatory, to use Apache HTTP Client
6053
- (version 4.3.2 or better), and if you have that on your classpath the `TestRestTemplate`
6054
- will respond by configuring the client appropriately.
6050
+ in a test-friendly way by not throwing exceptions on server-side errors. It is
6051
+ recommended, but not mandatory, to use Apache HTTP Client (version 4.3.2 or better), and
6052
+ if you have that on your classpath the `TestRestTemplate` will respond by configuring
6053
+ the client appropriately. If you do use Apache's HTTP client some additional test-friendly
6054
+ features will be enabled:
6055
+
6056
+ * Redirects will not be followed (so you can assert the response location)
6057
+ * Cookies will be ignored (so the template is stateless)
6058
+
6059
+ `TestRestTemplate` can be instantiated directly in your integration tests:
6055
6060
6056
6061
[source,java,indent=0]
6057
6062
----
@@ -6061,17 +6066,18 @@ public class MyTest {
6061
6066
6062
6067
@Test
6063
6068
public void testRequest() throws Exception {
6064
- HttpHeaders headers = template.getForEntity("http://myhost.com", String.class).getHeaders();
6069
+ HttpHeaders headers = template.getForEntity("http://myhost.com/example ", String.class).getHeaders();
6065
6070
assertThat(headers.getLocation().toString(), containsString("myotherhost"));
6066
6071
}
6067
6072
6068
6073
}
6069
6074
----
6070
6075
6071
- If you are using the `@SpringBootTest` annotation with `WebEnvironment.RANDOM_PORT` or
6072
- `WebEnvironment.DEFINED_PORT`, you can just inject a fully configured `TestRestTemplate`
6073
- and start using it. If necessary, additional customizations can be applied via the
6074
- `RestTemplateBuilder` bean:
6076
+ Alternatively, if you are using the `@SpringBootTest` annotation with
6077
+ `WebEnvironment.RANDOM_PORT` or `WebEnvironment.DEFINED_PORT`, you can just inject a
6078
+ fully configured `TestRestTemplate` and start using it. If necessary, additional
6079
+ customizations can be applied via the `RestTemplateBuilder` bean. Any URLs that do not
6080
+ specify a host and port will automatically connect to the embedded server:
6075
6081
6076
6082
[source,java,indent=0]
6077
6083
----
@@ -6084,7 +6090,7 @@ public class MyTest {
6084
6090
6085
6091
@Test
6086
6092
public void testRequest() throws Exception {
6087
- HttpHeaders headers = template.getForEntity("http://myhost.com ", String.class).getHeaders();
6093
+ HttpHeaders headers = template.getForEntity("/example ", String.class).getHeaders();
6088
6094
assertThat(headers.getLocation().toString(), containsString("myotherhost"));
6089
6095
}
6090
6096
0 commit comments