Rename RestTemplates to TestRestTemplate

Rename the RestTemplates to TestRestTemplate to help indicate that it's
primarily intended for testing. Also now extend RestTemplate to allow
direct use, rather than via factory methods.

Fixes gh-599
This commit is contained in:
Phillip Webb 2014-03-27 11:04:20 -07:00
parent d117a6b22b
commit aca67066bf
24 changed files with 141 additions and 128 deletions

View File

@ -1476,7 +1476,7 @@ interaction. For Example:
@Autowired @Autowired
CityRepository repository; CityRepository repository;
RestTemplate restTemplate = RestTemplates.get(); RestTemplate restTemplate = new TestRestTemplate();
// ... interact with the running server // ... interact with the running server
@ -1547,22 +1547,22 @@ public class MyTest {
---- ----
[[boot-features-rest-templates-test-utility]] [[boot-features-rest-templates-test-utility]]
==== RestTemplates ==== TestRestTemplate
`RestTemplates` is a static convenience factory for instances of `RestTemplate` that are `TestRestTemplate` is a convenience subclass of Spring's `RestTemplate` that is
useful in integration tests. You can get a vanilla template or one that sends Basic HTTP useful in integration tests. You can get a vanilla template or one that sends Basic HTTP
authentication (with a username and password). And in either case the template will behave authentication (with a username and password). And in either case the template will behave
in a friendly way for testing, not following redirects (so you can assert the response in a friendly way for testing, not following redirects (so you can assert the response
location), ignoring cookies (so the template is stateless), and not throwing exceptions location), ignoring cookies (so the template is stateless), and not throwing exceptions
on server-side errors. It is recommended, but not mandatory, to use Apache HTTP Client on server-side errors. It is recommended, but not mandatory, to use Apache HTTP Client
(version 4.3.2 or better), and if you have that on your classpath the `RestTemplates` will (version 4.3.2 or better), and if you have that on your classpath the `TestRestTemplate`
respond by configuring the client appropriately. will respond by configuring the client appropriately.
[source,java,indent=0] [source,java,indent=0]
---- ----
public class MyTest { public class MyTest {
RestTemplate template = RestTemplates.get(); RestTemplate template = new TestRestTemplate();
@Test @Test
public void testRequest() throws Exception { public void testRequest() throws Exception {

View File

@ -21,7 +21,7 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -46,7 +46,7 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080", Map.class); "http://localhost:8080", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -24,7 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -59,7 +59,7 @@ public class SampleActuatorUiApplicationPortTests {
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port, String.class); "http://localhost:" + this.port, String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
} }
@ -67,14 +67,14 @@ public class SampleActuatorUiApplicationPortTests {
@Test @Test
public void testMetrics() throws Exception { public void testMetrics() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.managementPort + "/metrics", Map.class); "http://localhost:" + this.managementPort + "/metrics", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
} }
@Test @Test
public void testHealth() throws Exception { public void testHealth() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.managementPort + "/health", String.class); "http://localhost:" + this.managementPort + "/health", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals("ok", entity.getBody()); assertEquals("ok", entity.getBody());

View File

@ -22,7 +22,7 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@ -53,7 +53,7 @@ public class SampleActuatorUiApplicationTests {
public void testHome() throws Exception { public void testHome() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> entity = RestTemplates.get().exchange( ResponseEntity<String> entity = new TestRestTemplate().exchange(
"http://localhost:8080", HttpMethod.GET, new HttpEntity<Void>(headers), "http://localhost:8080", HttpMethod.GET, new HttpEntity<Void>(headers),
String.class); String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@ -63,7 +63,7 @@ public class SampleActuatorUiApplicationTests {
@Test @Test
public void testCss() throws Exception { public void testCss() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080/css/bootstrap.min.css", String.class); "http://localhost:8080/css/bootstrap.min.css", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body")); assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body"));
@ -72,7 +72,7 @@ public class SampleActuatorUiApplicationTests {
@Test @Test
public void testMetrics() throws Exception { public void testMetrics() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080/metrics", Map.class); "http://localhost:8080/metrics", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
} }
@ -81,9 +81,9 @@ public class SampleActuatorUiApplicationTests {
public void testError() throws Exception { public void testError() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> entity = RestTemplates.get().exchange( ResponseEntity<String> entity = new TestRestTemplate().exchange(
"http://localhost:8080/error", HttpMethod.GET, "http://localhost:8080/error", HttpMethod.GET, new HttpEntity<Void>(
new HttpEntity<Void>(headers), String.class); headers), String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody() assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody()
.contains("<html>")); .contains("<html>"));

View File

@ -21,8 +21,8 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
@ -48,8 +48,8 @@ public class EndpointsPropertiesSampleActuatorApplicationTests {
@Test @Test
public void testCustomErrorPath() throws Exception { public void testCustomErrorPath() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get("user", "password").getForEntity( ResponseEntity<Map> entity = new TestRestTemplate("user", "password")
"http://localhost:8080/oops", Map.class); .getForEntity("http://localhost:8080/oops", Map.class);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
@ -59,7 +59,7 @@ public class EndpointsPropertiesSampleActuatorApplicationTests {
@Test @Test
public void testCustomContextPath() throws Exception { public void testCustomContextPath() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080/admin/health", String.class); "http://localhost:8080/admin/health", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
String body = entity.getBody(); String body = entity.getBody();

View File

@ -24,8 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
@ -60,14 +60,14 @@ public class ManagementAddressActuatorApplicationTests {
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port, Map.class); "http://localhost:" + this.port, Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
} }
@Test @Test
public void testHealth() throws Exception { public void testHealth() throws Exception {
ResponseEntity<String> entity = RestTemplates.get() ResponseEntity<String> entity = new TestRestTemplate()
.getForEntity( .getForEntity(
"http://localhost:" + this.managementPort + "/admin/health", "http://localhost:" + this.managementPort + "/admin/health",
String.class); String.class);

View File

@ -24,8 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
@ -60,7 +60,7 @@ public class ManagementPortSampleActuatorApplicationTests {
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get("user", getPassword()) ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port, Map.class); .getForEntity("http://localhost:" + this.port, Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -72,14 +72,14 @@ public class ManagementPortSampleActuatorApplicationTests {
public void testMetrics() throws Exception { public void testMetrics() throws Exception {
testHome(); // makes sure some requests have been made testHome(); // makes sure some requests have been made
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.managementPort + "/metrics", Map.class); "http://localhost:" + this.managementPort + "/metrics", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
} }
@Test @Test
public void testHealth() throws Exception { public void testHealth() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.managementPort + "/health", String.class); "http://localhost:" + this.managementPort + "/health", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals("ok", entity.getBody()); assertEquals("ok", entity.getBody());
@ -88,7 +88,7 @@ public class ManagementPortSampleActuatorApplicationTests {
@Test @Test
public void testErrorPage() throws Exception { public void testErrorPage() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.managementPort + "/error", Map.class); "http://localhost:" + this.managementPort + "/error", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -23,8 +23,8 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
@ -53,7 +53,7 @@ public class NoManagementSampleActuatorApplicationTests {
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get("user", getPassword()) ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:8080", Map.class); .getForEntity("http://localhost:8080", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -65,7 +65,7 @@ public class NoManagementSampleActuatorApplicationTests {
public void testMetricsNotAvailable() throws Exception { public void testMetricsNotAvailable() throws Exception {
testHome(); // makes sure some requests have been made testHome(); // makes sure some requests have been made
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get("user", getPassword()) ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:8080/metrics", Map.class); .getForEntity("http://localhost:8080/metrics", Map.class);
assertEquals(HttpStatus.NOT_FOUND, entity.getStatusCode()); assertEquals(HttpStatus.NOT_FOUND, entity.getStatusCode());
} }

View File

@ -25,8 +25,8 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
@ -60,7 +60,7 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testHomeIsSecure() throws Exception { public void testHomeIsSecure() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080", Map.class); "http://localhost:8080", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -73,7 +73,7 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get("user", getPassword()) ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:8080", Map.class); .getForEntity("http://localhost:8080", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -85,7 +85,7 @@ public class SampleActuatorApplicationTests {
public void testMetrics() throws Exception { public void testMetrics() throws Exception {
testHome(); // makes sure some requests have been made testHome(); // makes sure some requests have been made
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get("user", getPassword()) ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:8080/metrics", Map.class); .getForEntity("http://localhost:8080/metrics", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -96,7 +96,7 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testEnv() throws Exception { public void testEnv() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get("user", getPassword()) ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:8080/env", Map.class); .getForEntity("http://localhost:8080/env", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -106,7 +106,7 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testHealth() throws Exception { public void testHealth() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080/health", String.class); "http://localhost:8080/health", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals("ok", entity.getBody()); assertEquals("ok", entity.getBody());
@ -114,7 +114,7 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testErrorPage() throws Exception { public void testErrorPage() throws Exception {
ResponseEntity<String> entity = RestTemplates.get("user", getPassword()) ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:8080/foo", String.class); .getForEntity("http://localhost:8080/foo", String.class);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
String body = entity.getBody(); String body = entity.getBody();
@ -127,7 +127,7 @@ public class SampleActuatorApplicationTests {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
HttpEntity<?> request = new HttpEntity<Void>(headers); HttpEntity<?> request = new HttpEntity<Void>(headers);
ResponseEntity<String> entity = RestTemplates.get("user", getPassword()) ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
.exchange("http://localhost:8080/foo", HttpMethod.GET, request, .exchange("http://localhost:8080/foo", HttpMethod.GET, request,
String.class); String.class);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
@ -139,9 +139,9 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testTrace() throws Exception { public void testTrace() throws Exception {
RestTemplates.get().getForEntity("http://localhost:8080/health", String.class); new TestRestTemplate().getForEntity("http://localhost:8080/health", String.class);
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<List> entity = RestTemplates.get("user", getPassword()) ResponseEntity<List> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:8080/trace", List.class); .getForEntity("http://localhost:8080/trace", List.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -156,7 +156,7 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testErrorPageDirectAccess() throws Exception { public void testErrorPageDirectAccess() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080/error", Map.class); "http://localhost:8080/error", Map.class);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -168,7 +168,7 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testBeans() throws Exception { public void testBeans() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<List> entity = RestTemplates.get("user", getPassword()) ResponseEntity<List> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:8080/beans", List.class); .getForEntity("http://localhost:8080/beans", List.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals(1, entity.getBody().size()); assertEquals(1, entity.getBody().size());

View File

@ -23,7 +23,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -52,7 +52,7 @@ public class ShutdownSampleActuatorApplicationTests {
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get("user", getPassword()) ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:8080", Map.class); .getForEntity("http://localhost:8080", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -63,7 +63,7 @@ public class ShutdownSampleActuatorApplicationTests {
@Test @Test
public void testShutdown() throws Exception { public void testShutdown() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get("user", getPassword()) ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.postForEntity("http://localhost:8080/shutdown", null, Map.class); .postForEntity("http://localhost:8080/shutdown", null, Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -21,7 +21,7 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -51,7 +51,7 @@ public class UnsecureManagementSampleActuatorApplicationTests {
@Test @Test
public void testHomeIsSecure() throws Exception { public void testHomeIsSecure() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080", Map.class); "http://localhost:8080", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -70,7 +70,7 @@ public class UnsecureManagementSampleActuatorApplicationTests {
// ignore; // ignore;
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080/metrics", Map.class); "http://localhost:8080/metrics", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -21,7 +21,7 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -50,7 +50,7 @@ public class UnsecureSampleActuatorApplicationTests {
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = RestTemplates.get().getForEntity( ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080", Map.class); "http://localhost:8080", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -19,7 +19,7 @@ package sample.jetty;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -43,7 +43,7 @@ public class SampleJettyApplicationTests {
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080", String.class); "http://localhost:8080", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals("Hello World", entity.getBody()); assertEquals("Hello World", entity.getBody());

View File

@ -21,7 +21,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -48,14 +48,14 @@ public class SampleServletApplicationTests {
@Test @Test
public void testHomeIsSecure() throws Exception { public void testHomeIsSecure() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080", String.class); "http://localhost:8080", String.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
} }
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = RestTemplates.get("user", getPassword()) ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:8080", String.class); .getForEntity("http://localhost:8080", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals("Hello World", entity.getBody()); assertEquals("Hello World", entity.getBody());

View File

@ -26,7 +26,7 @@ import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfi
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -72,7 +72,7 @@ public class NonAutoConfigurationSampleTomcatApplicationTests {
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080", String.class); "http://localhost:8080", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals("Hello World", entity.getBody()); assertEquals("Hello World", entity.getBody());

View File

@ -19,7 +19,7 @@ package sample.tomcat;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -43,7 +43,7 @@ public class SampleTomcatApplicationTests {
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080", String.class); "http://localhost:8080", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals("Hello World", entity.getBody()); assertEquals("Hello World", entity.getBody());

View File

@ -19,7 +19,7 @@ package sample.traditional;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -44,7 +44,7 @@ public class SampleTraditionalApplicationTests {
@Test @Test
public void testHomeJsp() throws Exception { public void testHomeJsp() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080", String.class); "http://localhost:8080", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
String body = entity.getBody(); String body = entity.getBody();
@ -54,7 +54,7 @@ public class SampleTraditionalApplicationTests {
@Test @Test
public void testStaticPage() throws Exception { public void testStaticPage() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080/index.html", String.class); "http://localhost:8080/index.html", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
String body = entity.getBody(); String body = entity.getBody();

View File

@ -19,7 +19,7 @@ package sample.jsp;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -44,7 +44,7 @@ public class SampleWebJspApplicationTests {
@Test @Test
public void testJspWithEl() throws Exception { public void testJspWithEl() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080", String.class); "http://localhost:8080", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body:\n" + entity.getBody(), assertTrue("Wrong body:\n" + entity.getBody(),

View File

@ -23,7 +23,7 @@ import java.util.regex.Pattern;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@ -56,7 +56,7 @@ public class SampleMethodSecurityApplicationTests {
public void testHome() throws Exception { public void testHome() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> entity = RestTemplates.get().exchange( ResponseEntity<String> entity = new TestRestTemplate().exchange(
"http://localhost:8080", HttpMethod.GET, new HttpEntity<Void>(headers), "http://localhost:8080", HttpMethod.GET, new HttpEntity<Void>(headers),
String.class); String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@ -72,7 +72,7 @@ public class SampleMethodSecurityApplicationTests {
form.set("username", "admin"); form.set("username", "admin");
form.set("password", "admin"); form.set("password", "admin");
getCsrf(form, headers); getCsrf(form, headers);
ResponseEntity<String> entity = RestTemplates.get().exchange( ResponseEntity<String> entity = new TestRestTemplate().exchange(
"http://localhost:8080/login", HttpMethod.POST, "http://localhost:8080/login", HttpMethod.POST,
new HttpEntity<MultiValueMap<String, String>>(form, headers), new HttpEntity<MultiValueMap<String, String>>(form, headers),
String.class); String.class);
@ -89,23 +89,23 @@ public class SampleMethodSecurityApplicationTests {
form.set("username", "user"); form.set("username", "user");
form.set("password", "user"); form.set("password", "user");
getCsrf(form, headers); getCsrf(form, headers);
ResponseEntity<String> entity = RestTemplates.get().exchange( ResponseEntity<String> entity = new TestRestTemplate().exchange(
"http://localhost:8080/login", HttpMethod.POST, "http://localhost:8080/login", HttpMethod.POST,
new HttpEntity<MultiValueMap<String, String>>(form, headers), new HttpEntity<MultiValueMap<String, String>>(form, headers),
String.class); String.class);
assertEquals(HttpStatus.FOUND, entity.getStatusCode()); assertEquals(HttpStatus.FOUND, entity.getStatusCode());
String cookie = entity.getHeaders().getFirst("Set-Cookie"); String cookie = entity.getHeaders().getFirst("Set-Cookie");
headers.set("Cookie", cookie); headers.set("Cookie", cookie);
ResponseEntity<String> page = RestTemplates.get().exchange( ResponseEntity<String> page = new TestRestTemplate().exchange(entity.getHeaders()
entity.getHeaders().getLocation(), HttpMethod.GET, .getLocation(), HttpMethod.GET, new HttpEntity<Void>(headers),
new HttpEntity<Void>(headers), String.class); String.class);
assertEquals(HttpStatus.FORBIDDEN, page.getStatusCode()); assertEquals(HttpStatus.FORBIDDEN, page.getStatusCode());
assertTrue("Wrong body (message doesn't match):\n" + entity.getBody(), page assertTrue("Wrong body (message doesn't match):\n" + entity.getBody(), page
.getBody().contains("Access denied")); .getBody().contains("Access denied"));
} }
private void getCsrf(MultiValueMap<String, String> form, HttpHeaders headers) { private void getCsrf(MultiValueMap<String, String> form, HttpHeaders headers) {
ResponseEntity<String> page = RestTemplates.get().getForEntity( ResponseEntity<String> page = new TestRestTemplate().getForEntity(
"http://localhost:8080/login", String.class); "http://localhost:8080/login", String.class);
String cookie = page.getHeaders().getFirst("Set-Cookie"); String cookie = page.getHeaders().getFirst("Set-Cookie");
headers.set("Cookie", cookie); headers.set("Cookie", cookie);

View File

@ -21,7 +21,7 @@ import java.util.Arrays;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@ -52,7 +52,7 @@ public class SampleSecureApplicationTests {
public void testHome() throws Exception { public void testHome() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> entity = RestTemplates.get().exchange( ResponseEntity<String> entity = new TestRestTemplate().exchange(
"http://localhost:8080", HttpMethod.GET, new HttpEntity<Void>(headers), "http://localhost:8080", HttpMethod.GET, new HttpEntity<Void>(headers),
String.class); String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
@ -62,7 +62,7 @@ public class SampleSecureApplicationTests {
@Test @Test
public void testCss() throws Exception { public void testCss() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080/css/bootstrap.min.css", String.class); "http://localhost:8080/css/bootstrap.min.css", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body")); assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body"));

View File

@ -19,7 +19,7 @@ package sample.ui;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@ -45,7 +45,7 @@ public class SampleWebStaticApplicationTests {
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080", String.class); "http://localhost:8080", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity
@ -54,7 +54,7 @@ public class SampleWebStaticApplicationTests {
@Test @Test
public void testCss() throws Exception { public void testCss() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080/webjars/bootstrap/3.0.3/css/bootstrap.min.css", "http://localhost:8080/webjars/bootstrap/3.0.3/css/bootstrap.min.css",
String.class); String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());

View File

@ -21,7 +21,7 @@ import java.net.URI;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -49,7 +49,7 @@ public class SampleWebUiApplicationTests {
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080", String.class); "http://localhost:8080", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity
@ -63,14 +63,15 @@ public class SampleWebUiApplicationTests {
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.set("text", "FOO text"); map.set("text", "FOO text");
map.set("summary", "FOO"); map.set("summary", "FOO");
URI location = RestTemplates.get().postForLocation("http://localhost:8080", map); URI location = new TestRestTemplate().postForLocation("http://localhost:8080",
map);
assertTrue("Wrong location:\n" + location, assertTrue("Wrong location:\n" + location,
location.toString().contains("localhost:8080")); location.toString().contains("localhost:8080"));
} }
@Test @Test
public void testCss() throws Exception { public void testCss() throws Exception {
ResponseEntity<String> entity = RestTemplates.get().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:8080/css/bootstrap.min.css", String.class); "http://localhost:8080/css/bootstrap.min.css", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body")); assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body"));

View File

@ -52,7 +52,7 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
* <p> * <p>
* If you <em>want</em> to start a web server, mark the test class as * If you <em>want</em> to start a web server, mark the test class as
* <code>@WebAppConfiguration @IntegrationTest</code>. This is useful for testing HTTP * <code>@WebAppConfiguration @IntegrationTest</code>. This is useful for testing HTTP
* endpoints using {@link RestTemplates} (for instance), especially since you can * endpoints using {@link TestRestTemplate} (for instance), especially since you can
* <code>@Autowired</code> application context components into your test case to see the * <code>@Autowired</code> application context components into your test case to see the
* internal effects of HTTP requests directly. * internal effects of HTTP requests directly.
* <p> * <p>

View File

@ -18,7 +18,7 @@ package org.springframework.boot.test;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.ArrayList; import java.util.Collections;
import java.util.List; import java.util.List;
import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.config.CookieSpecs;
@ -29,6 +29,7 @@ import org.apache.http.protocol.HttpContext;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest; import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
@ -39,62 +40,73 @@ import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
/** /**
* Convenient static factory for {@link RestTemplate} instances that are suitable for * Convenient subclass of {@link RestTemplate} that is suitable for integration tests.
* integration tests. They are fault tolerant, and optionally can carry Basic * They are fault tolerant, and optionally can carry Basic authentication headers. If
* authentication headers. If Apache Http Client 4.3.2 or better is available * Apache Http Client 4.3.2 or better is available (recommended) it will be used as the
* (recommended) it will be used as the client, and configured to ignore cookies and * client, and configured to ignore cookies and redirects.
* redirects.
* *
* @author Dave Syer * @author Dave Syer
* @author Phillip Webb
*/ */
public class RestTemplates { public class TestRestTemplate extends RestTemplate {
/** /**
* Basic factory method for a RestTemplate that does not follow redirects, ignores * Create a new {@link TestRestTemplate} instance.
* cookies and does not throw exceptions on server side errors.
* @return a basic RestTemplate with no authentication
*/ */
public static RestTemplate get() { public TestRestTemplate() {
return get(null, null); this(null, null);
} }
/** /**
* Factory method for a secure RestTemplate with Basic authentication that does not * Create a new {@link TestRestTemplate} instance with the specified credentials.
* follow redirects, ignores cookies and does not throw exceptions on server side * @param username the username to use (or {@code null})
* errors. * @param password the password (or {@code null})
* @return a basic RestTemplate with Basic authentication
*/ */
public static RestTemplate get(final String username, final String password) { public TestRestTemplate(String username, String password) {
super(getRequestFactory(username, password));
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
if (username != null) {
interceptors.add(new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
byte[] token = Base64.encode((username + ":" + password).getBytes());
request.getHeaders().add("Authorization",
"Basic " + new String(token));
return execution.execute(request, body);
}
});
}
RestTemplate restTemplate = new RestTemplate(
new InterceptingClientHttpRequestFactory(
new SimpleClientHttpRequestFactory(), interceptors));
if (ClassUtils.isPresent("org.apache.http.client.config.RequestConfig", null)) { if (ClassUtils.isPresent("org.apache.http.client.config.RequestConfig", null)) {
new HttpComponentsCustomizer().customize(restTemplate); new HttpComponentsCustomizer().customize(this);
} }
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { setErrorHandler(new DefaultResponseErrorHandler() {
@Override @Override
public void handleError(ClientHttpResponse response) throws IOException { public void handleError(ClientHttpResponse response) throws IOException {
} }
}); });
return restTemplate;
}
private static ClientHttpRequestFactory getRequestFactory(String username,
String password) {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
if (username == null) {
return factory;
}
List<ClientHttpRequestInterceptor> interceptors = Collections
.<ClientHttpRequestInterceptor> singletonList(new BasicAuthorizationInterceptor(
username, password));
return new InterceptingClientHttpRequestFactory(factory, interceptors);
}
private static class BasicAuthorizationInterceptor implements
ClientHttpRequestInterceptor {
private final String username;
private final String password;
public BasicAuthorizationInterceptor(String username, String password) {
this.username = username;
this.password = (password == null ? "" : password);
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
byte[] token = Base64
.encode((this.username + ":" + this.password).getBytes());
request.getHeaders().add("Authorization", "Basic " + new String(token));
return execution.execute(request, body);
}
} }