Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
cdebfcde
Commit
cdebfcde
authored
Jun 06, 2017
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.5.x'
parents
9464eea7
5be5b137
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
11 deletions
+17
-11
spring-boot-features.adoc
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
+17
-11
No files found.
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
View file @
cdebfcde
...
@@ -6258,11 +6258,16 @@ public class MyTest {
...
@@ -6258,11 +6258,16 @@ public class MyTest {
`TestRestTemplate` is a convenience alternative to Spring's `RestTemplate` that is useful
`TestRestTemplate` is a convenience alternative to Spring's `RestTemplate` that is useful
in integration tests. You can get a vanilla template or one that sends Basic HTTP
in integration tests. You can get a vanilla template or one that sends Basic HTTP
authentication (with a username and password). In either case the template will behave
authentication (with a username and password). In either case the template will behave
in a test-friendly way: not following redirects (so you can assert the response location),
in a test-friendly way by not throwing exceptions on server-side errors. It is
ignoring cookies (so the template is stateless), and not throwing exceptions on
recommended, but not mandatory, to use Apache HTTP Client (version 4.3.2 or better), and
server-side errors. It is recommended, but not mandatory, to use Apache HTTP Client
if you have that on your classpath the `TestRestTemplate` will respond by configuring
(version 4.3.2 or better), and if you have that on your classpath the `TestRestTemplate`
the client appropriately. If you do use Apache's HTTP client some additional test-friendly
will respond by configuring the client appropriately.
features will be enabled:
* Redirects will not be followed (so you can assert the response location)
* Cookies will be ignored (so the template is stateless)
`TestRestTemplate` can be instantiated directly in your integration tests:
[source,java,indent=0]
[source,java,indent=0]
----
----
...
@@ -6272,17 +6277,18 @@ public class MyTest {
...
@@ -6272,17 +6277,18 @@ public class MyTest {
@Test
@Test
public void testRequest() throws Exception {
public void testRequest() throws Exception {
HttpHeaders headers = template.getForEntity("http://myhost.com", String.class).getHeaders();
HttpHeaders headers = template.getForEntity("http://myhost.com
/example
", String.class).getHeaders();
assertThat(headers.getLocation().toString(), containsString("myotherhost"));
assertThat(headers.getLocation().toString(), containsString("myotherhost"));
}
}
}
}
----
----
If you are using the `@SpringBootTest` annotation with `WebEnvironment.RANDOM_PORT` or
Alternatively, if you are using the `@SpringBootTest` annotation with
`WebEnvironment.DEFINED_PORT`, you can just inject a fully configured `TestRestTemplate`
`WebEnvironment.RANDOM_PORT` or `WebEnvironment.DEFINED_PORT`, you can just inject a
and start using it. If necessary, additional customizations can be applied via the
fully configured `TestRestTemplate` and start using it. If necessary, additional
`RestTemplateBuilder` bean:
customizations can be applied via the `RestTemplateBuilder` bean. Any URLs that do not
specify a host and port will automatically connect to the embedded server:
[source,java,indent=0]
[source,java,indent=0]
----
----
...
@@ -6295,7 +6301,7 @@ public class MyTest {
...
@@ -6295,7 +6301,7 @@ public class MyTest {
@Test
@Test
public void testRequest() throws Exception {
public void testRequest() throws Exception {
HttpHeaders headers = template.getForEntity("
http://myhost.com
", String.class).getHeaders();
HttpHeaders headers = template.getForEntity("
/example
", String.class).getHeaders();
assertThat(headers.getLocation().toString(), containsString("myotherhost"));
assertThat(headers.getLocation().toString(), containsString("myotherhost"));
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment