From aca67066bf0e5dc72d5864cf3dc842ec2e53d724 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 27 Mar 2014 11:04:20 -0700 Subject: [PATCH] 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 --- .../main/asciidoc/spring-boot-features.adoc | 12 +-- .../log4j/SampleActuatorApplicationTests.java | 4 +- .../SampleActuatorUiApplicationPortTests.java | 8 +- .../ui/SampleActuatorUiApplicationTests.java | 14 +-- ...pertiesSampleActuatorApplicationTests.java | 8 +- ...gementAddressActuatorApplicationTests.java | 6 +- ...entPortSampleActuatorApplicationTests.java | 10 +- ...agementSampleActuatorApplicationTests.java | 6 +- .../SampleActuatorApplicationTests.java | 24 ++--- ...hutdownSampleActuatorApplicationTests.java | 6 +- ...agementSampleActuatorApplicationTests.java | 6 +- ...nsecureSampleActuatorApplicationTests.java | 4 +- .../jetty/SampleJettyApplicationTests.java | 4 +- .../SampleServletApplicationTests.java | 6 +- ...igurationSampleTomcatApplicationTests.java | 4 +- .../tomcat/SampleTomcatApplicationTests.java | 4 +- .../SampleTraditionalApplicationTests.java | 6 +- .../jsp/SampleWebJspApplicationTests.java | 4 +- .../SampleMethodSecurityApplicationTests.java | 16 ++-- .../secure/SampleSecureApplicationTests.java | 6 +- .../ui/SampleWebStaticApplicationTests.java | 6 +- .../ui/SampleWebUiApplicationTests.java | 9 +- .../test/SpringApplicationContextLoader.java | 2 +- ...stTemplates.java => TestRestTemplate.java} | 94 +++++++++++-------- 24 files changed, 141 insertions(+), 128 deletions(-) rename spring-boot/src/main/java/org/springframework/boot/test/{RestTemplates.java => TestRestTemplate.java} (53%) 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 2e2605f9cb..633f918f4e 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -1476,7 +1476,7 @@ interaction. For Example: @Autowired CityRepository repository; - RestTemplate restTemplate = RestTemplates.get(); + RestTemplate restTemplate = new TestRestTemplate(); // ... interact with the running server @@ -1547,22 +1547,22 @@ public class MyTest { ---- [[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 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 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 -(version 4.3.2 or better), and if you have that on your classpath the `RestTemplates` will -respond by configuring the client appropriately. +(version 4.3.2 or better), and if you have that on your classpath the `TestRestTemplate` +will respond by configuring the client appropriately. [source,java,indent=0] ---- public class MyTest { - RestTemplate template = RestTemplates.get(); + RestTemplate template = new TestRestTemplate(); @Test public void testRequest() throws Exception { diff --git a/spring-boot-samples/spring-boot-sample-actuator-log4j/src/test/java/sample/actuator/log4j/SampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator-log4j/src/test/java/sample/actuator/log4j/SampleActuatorApplicationTests.java index 35d914b029..a7ec5dcc2f 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-log4j/src/test/java/sample/actuator/log4j/SampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator-log4j/src/test/java/sample/actuator/log4j/SampleActuatorApplicationTests.java @@ -21,7 +21,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; 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.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -46,7 +46,7 @@ public class SampleActuatorApplicationTests { @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080", Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") diff --git a/spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationPortTests.java b/spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationPortTests.java index 09861b1297..b26cc3ad47 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationPortTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationPortTests.java @@ -24,7 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.security.SecurityProperties; 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.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -59,7 +59,7 @@ public class SampleActuatorUiApplicationPortTests { @Test public void testHome() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:" + this.port, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); } @@ -67,14 +67,14 @@ public class SampleActuatorUiApplicationPortTests { @Test public void testMetrics() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:" + this.managementPort + "/metrics", Map.class); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); } @Test public void testHealth() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:" + this.managementPort + "/health", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("ok", entity.getBody()); diff --git a/spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationTests.java index f199ac9182..f0472edb18 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationTests.java @@ -22,7 +22,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; 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.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -53,7 +53,7 @@ public class SampleActuatorUiApplicationTests { public void testHome() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); - ResponseEntity entity = RestTemplates.get().exchange( + ResponseEntity entity = new TestRestTemplate().exchange( "http://localhost:8080", HttpMethod.GET, new HttpEntity(headers), String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @@ -63,7 +63,7 @@ public class SampleActuatorUiApplicationTests { @Test public void testCss() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080/css/bootstrap.min.css", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body")); @@ -72,7 +72,7 @@ public class SampleActuatorUiApplicationTests { @Test public void testMetrics() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080/metrics", Map.class); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); } @@ -81,9 +81,9 @@ public class SampleActuatorUiApplicationTests { public void testError() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); - ResponseEntity entity = RestTemplates.get().exchange( - "http://localhost:8080/error", HttpMethod.GET, - new HttpEntity(headers), String.class); + ResponseEntity entity = new TestRestTemplate().exchange( + "http://localhost:8080/error", HttpMethod.GET, new HttpEntity( + headers), String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody() .contains("")); diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/EndpointsPropertiesSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/EndpointsPropertiesSampleActuatorApplicationTests.java index b0d34cb676..cf2ae71f55 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/EndpointsPropertiesSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/EndpointsPropertiesSampleActuatorApplicationTests.java @@ -21,8 +21,8 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.IntegrationTest; -import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; @@ -48,8 +48,8 @@ public class EndpointsPropertiesSampleActuatorApplicationTests { @Test public void testCustomErrorPath() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get("user", "password").getForEntity( - "http://localhost:8080/oops", Map.class); + ResponseEntity entity = new TestRestTemplate("user", "password") + .getForEntity("http://localhost:8080/oops", Map.class); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); @SuppressWarnings("unchecked") Map body = entity.getBody(); @@ -59,7 +59,7 @@ public class EndpointsPropertiesSampleActuatorApplicationTests { @Test public void testCustomContextPath() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080/admin/health", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); String body = entity.getBody(); diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementAddressActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementAddressActuatorApplicationTests.java index dcbee314fd..bc734fd3cd 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementAddressActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementAddressActuatorApplicationTests.java @@ -24,8 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.test.IntegrationTest; -import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; @@ -60,14 +60,14 @@ public class ManagementAddressActuatorApplicationTests { @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:" + this.port, Map.class); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); } @Test public void testHealth() throws Exception { - ResponseEntity entity = RestTemplates.get() + ResponseEntity entity = new TestRestTemplate() .getForEntity( "http://localhost:" + this.managementPort + "/admin/health", String.class); diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPortSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPortSampleActuatorApplicationTests.java index f3e33779ba..715f8f77e6 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPortSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPortSampleActuatorApplicationTests.java @@ -24,8 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.test.IntegrationTest; -import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; @@ -60,7 +60,7 @@ public class ManagementPortSampleActuatorApplicationTests { @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get("user", getPassword()) + ResponseEntity entity = new TestRestTemplate("user", getPassword()) .getForEntity("http://localhost:" + this.port, Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") @@ -72,14 +72,14 @@ public class ManagementPortSampleActuatorApplicationTests { public void testMetrics() throws Exception { testHome(); // makes sure some requests have been made @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:" + this.managementPort + "/metrics", Map.class); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); } @Test public void testHealth() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:" + this.managementPort + "/health", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("ok", entity.getBody()); @@ -88,7 +88,7 @@ public class ManagementPortSampleActuatorApplicationTests { @Test public void testErrorPage() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:" + this.managementPort + "/error", Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NoManagementSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NoManagementSampleActuatorApplicationTests.java index 4f9e3c354d..7ad5d0e470 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NoManagementSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NoManagementSampleActuatorApplicationTests.java @@ -23,8 +23,8 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.test.IntegrationTest; -import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; @@ -53,7 +53,7 @@ public class NoManagementSampleActuatorApplicationTests { @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get("user", getPassword()) + ResponseEntity entity = new TestRestTemplate("user", getPassword()) .getForEntity("http://localhost:8080", Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") @@ -65,7 +65,7 @@ public class NoManagementSampleActuatorApplicationTests { public void testMetricsNotAvailable() throws Exception { testHome(); // makes sure some requests have been made @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get("user", getPassword()) + ResponseEntity entity = new TestRestTemplate("user", getPassword()) .getForEntity("http://localhost:8080/metrics", Map.class); assertEquals(HttpStatus.NOT_FOUND, entity.getStatusCode()); } diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java index 9ccc06cbbc..8b0325874b 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java @@ -25,8 +25,8 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.test.IntegrationTest; -import org.springframework.boot.test.RestTemplates; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -60,7 +60,7 @@ public class SampleActuatorApplicationTests { @Test public void testHomeIsSecure() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080", Map.class); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); @SuppressWarnings("unchecked") @@ -73,7 +73,7 @@ public class SampleActuatorApplicationTests { @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get("user", getPassword()) + ResponseEntity entity = new TestRestTemplate("user", getPassword()) .getForEntity("http://localhost:8080", Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") @@ -85,7 +85,7 @@ public class SampleActuatorApplicationTests { public void testMetrics() throws Exception { testHome(); // makes sure some requests have been made @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get("user", getPassword()) + ResponseEntity entity = new TestRestTemplate("user", getPassword()) .getForEntity("http://localhost:8080/metrics", Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") @@ -96,7 +96,7 @@ public class SampleActuatorApplicationTests { @Test public void testEnv() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get("user", getPassword()) + ResponseEntity entity = new TestRestTemplate("user", getPassword()) .getForEntity("http://localhost:8080/env", Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") @@ -106,7 +106,7 @@ public class SampleActuatorApplicationTests { @Test public void testHealth() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080/health", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("ok", entity.getBody()); @@ -114,7 +114,7 @@ public class SampleActuatorApplicationTests { @Test public void testErrorPage() throws Exception { - ResponseEntity entity = RestTemplates.get("user", getPassword()) + ResponseEntity entity = new TestRestTemplate("user", getPassword()) .getForEntity("http://localhost:8080/foo", String.class); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); String body = entity.getBody(); @@ -127,7 +127,7 @@ public class SampleActuatorApplicationTests { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); HttpEntity request = new HttpEntity(headers); - ResponseEntity entity = RestTemplates.get("user", getPassword()) + ResponseEntity entity = new TestRestTemplate("user", getPassword()) .exchange("http://localhost:8080/foo", HttpMethod.GET, request, String.class); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); @@ -139,9 +139,9 @@ public class SampleActuatorApplicationTests { @Test 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") - ResponseEntity entity = RestTemplates.get("user", getPassword()) + ResponseEntity entity = new TestRestTemplate("user", getPassword()) .getForEntity("http://localhost:8080/trace", List.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") @@ -156,7 +156,7 @@ public class SampleActuatorApplicationTests { @Test public void testErrorPageDirectAccess() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080/error", Map.class); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); @SuppressWarnings("unchecked") @@ -168,7 +168,7 @@ public class SampleActuatorApplicationTests { @Test public void testBeans() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get("user", getPassword()) + ResponseEntity entity = new TestRestTemplate("user", getPassword()) .getForEntity("http://localhost:8080/beans", List.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(1, entity.getBody().size()); diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ShutdownSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ShutdownSampleActuatorApplicationTests.java index 6e58abd58b..2a3532c17d 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ShutdownSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ShutdownSampleActuatorApplicationTests.java @@ -23,7 +23,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.security.SecurityProperties; 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.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -52,7 +52,7 @@ public class ShutdownSampleActuatorApplicationTests { @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get("user", getPassword()) + ResponseEntity entity = new TestRestTemplate("user", getPassword()) .getForEntity("http://localhost:8080", Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") @@ -63,7 +63,7 @@ public class ShutdownSampleActuatorApplicationTests { @Test public void testShutdown() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get("user", getPassword()) + ResponseEntity entity = new TestRestTemplate("user", getPassword()) .postForEntity("http://localhost:8080/shutdown", null, Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureManagementSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureManagementSampleActuatorApplicationTests.java index 617d6d65e3..ed9fb6561b 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureManagementSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureManagementSampleActuatorApplicationTests.java @@ -21,7 +21,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; 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.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -51,7 +51,7 @@ public class UnsecureManagementSampleActuatorApplicationTests { @Test public void testHomeIsSecure() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080", Map.class); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); @SuppressWarnings("unchecked") @@ -70,7 +70,7 @@ public class UnsecureManagementSampleActuatorApplicationTests { // ignore; } @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080/metrics", Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureSampleActuatorApplicationTests.java index 24e0005113..6a401a42bb 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/UnsecureSampleActuatorApplicationTests.java @@ -21,7 +21,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; 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.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -50,7 +50,7 @@ public class UnsecureSampleActuatorApplicationTests { @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080", Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") diff --git a/spring-boot-samples/spring-boot-sample-jetty/src/test/java/sample/jetty/SampleJettyApplicationTests.java b/spring-boot-samples/spring-boot-sample-jetty/src/test/java/sample/jetty/SampleJettyApplicationTests.java index 4e485b3685..9d95cce365 100644 --- a/spring-boot-samples/spring-boot-sample-jetty/src/test/java/sample/jetty/SampleJettyApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-jetty/src/test/java/sample/jetty/SampleJettyApplicationTests.java @@ -19,7 +19,7 @@ package sample.jetty; import org.junit.Test; import org.junit.runner.RunWith; 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.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -43,7 +43,7 @@ public class SampleJettyApplicationTests { @Test public void testHome() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("Hello World", entity.getBody()); diff --git a/spring-boot-samples/spring-boot-sample-servlet/src/test/java/sample/servlet/SampleServletApplicationTests.java b/spring-boot-samples/spring-boot-sample-servlet/src/test/java/sample/servlet/SampleServletApplicationTests.java index 2f815d3b91..6733934ddc 100644 --- a/spring-boot-samples/spring-boot-sample-servlet/src/test/java/sample/servlet/SampleServletApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-servlet/src/test/java/sample/servlet/SampleServletApplicationTests.java @@ -21,7 +21,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.security.SecurityProperties; 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.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -48,14 +48,14 @@ public class SampleServletApplicationTests { @Test public void testHomeIsSecure() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080", String.class); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); } @Test public void testHome() throws Exception { - ResponseEntity entity = RestTemplates.get("user", getPassword()) + ResponseEntity entity = new TestRestTemplate("user", getPassword()) .getForEntity("http://localhost:8080", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("Hello World", entity.getBody()); diff --git a/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/NonAutoConfigurationSampleTomcatApplicationTests.java b/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/NonAutoConfigurationSampleTomcatApplicationTests.java index 14400800c2..b3c5ab7a48 100644 --- a/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/NonAutoConfigurationSampleTomcatApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/NonAutoConfigurationSampleTomcatApplicationTests.java @@ -26,7 +26,7 @@ import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfi import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; 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.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -72,7 +72,7 @@ public class NonAutoConfigurationSampleTomcatApplicationTests { @Test public void testHome() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("Hello World", entity.getBody()); diff --git a/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java b/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java index 417b229544..2ed1307a13 100644 --- a/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java @@ -19,7 +19,7 @@ package sample.tomcat; import org.junit.Test; import org.junit.runner.RunWith; 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.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -43,7 +43,7 @@ public class SampleTomcatApplicationTests { @Test public void testHome() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("Hello World", entity.getBody()); diff --git a/spring-boot-samples/spring-boot-sample-traditional/src/test/java/sample/traditional/SampleTraditionalApplicationTests.java b/spring-boot-samples/spring-boot-sample-traditional/src/test/java/sample/traditional/SampleTraditionalApplicationTests.java index 1244e359b3..f5f3cabc92 100644 --- a/spring-boot-samples/spring-boot-sample-traditional/src/test/java/sample/traditional/SampleTraditionalApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-traditional/src/test/java/sample/traditional/SampleTraditionalApplicationTests.java @@ -19,7 +19,7 @@ package sample.traditional; import org.junit.Test; import org.junit.runner.RunWith; 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.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -44,7 +44,7 @@ public class SampleTraditionalApplicationTests { @Test public void testHomeJsp() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); String body = entity.getBody(); @@ -54,7 +54,7 @@ public class SampleTraditionalApplicationTests { @Test public void testStaticPage() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080/index.html", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); String body = entity.getBody(); diff --git a/spring-boot-samples/spring-boot-sample-web-jsp/src/test/java/sample/jsp/SampleWebJspApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-jsp/src/test/java/sample/jsp/SampleWebJspApplicationTests.java index ffd52a7e0d..ca9a593eb1 100644 --- a/spring-boot-samples/spring-boot-sample-web-jsp/src/test/java/sample/jsp/SampleWebJspApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-jsp/src/test/java/sample/jsp/SampleWebJspApplicationTests.java @@ -19,7 +19,7 @@ package sample.jsp; import org.junit.Test; import org.junit.runner.RunWith; 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.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -44,7 +44,7 @@ public class SampleWebJspApplicationTests { @Test public void testJspWithEl() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body:\n" + entity.getBody(), diff --git a/spring-boot-samples/spring-boot-sample-web-method-security/src/test/java/sample/ui/method/SampleMethodSecurityApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-method-security/src/test/java/sample/ui/method/SampleMethodSecurityApplicationTests.java index 82e8ff302b..0a3559e590 100644 --- a/spring-boot-samples/spring-boot-sample-web-method-security/src/test/java/sample/ui/method/SampleMethodSecurityApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-method-security/src/test/java/sample/ui/method/SampleMethodSecurityApplicationTests.java @@ -23,7 +23,7 @@ import java.util.regex.Pattern; import org.junit.Test; import org.junit.runner.RunWith; 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.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -56,7 +56,7 @@ public class SampleMethodSecurityApplicationTests { public void testHome() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); - ResponseEntity entity = RestTemplates.get().exchange( + ResponseEntity entity = new TestRestTemplate().exchange( "http://localhost:8080", HttpMethod.GET, new HttpEntity(headers), String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @@ -72,7 +72,7 @@ public class SampleMethodSecurityApplicationTests { form.set("username", "admin"); form.set("password", "admin"); getCsrf(form, headers); - ResponseEntity entity = RestTemplates.get().exchange( + ResponseEntity entity = new TestRestTemplate().exchange( "http://localhost:8080/login", HttpMethod.POST, new HttpEntity>(form, headers), String.class); @@ -89,23 +89,23 @@ public class SampleMethodSecurityApplicationTests { form.set("username", "user"); form.set("password", "user"); getCsrf(form, headers); - ResponseEntity entity = RestTemplates.get().exchange( + ResponseEntity entity = new TestRestTemplate().exchange( "http://localhost:8080/login", HttpMethod.POST, new HttpEntity>(form, headers), String.class); assertEquals(HttpStatus.FOUND, entity.getStatusCode()); String cookie = entity.getHeaders().getFirst("Set-Cookie"); headers.set("Cookie", cookie); - ResponseEntity page = RestTemplates.get().exchange( - entity.getHeaders().getLocation(), HttpMethod.GET, - new HttpEntity(headers), String.class); + ResponseEntity page = new TestRestTemplate().exchange(entity.getHeaders() + .getLocation(), HttpMethod.GET, new HttpEntity(headers), + String.class); assertEquals(HttpStatus.FORBIDDEN, page.getStatusCode()); assertTrue("Wrong body (message doesn't match):\n" + entity.getBody(), page .getBody().contains("Access denied")); } private void getCsrf(MultiValueMap form, HttpHeaders headers) { - ResponseEntity page = RestTemplates.get().getForEntity( + ResponseEntity page = new TestRestTemplate().getForEntity( "http://localhost:8080/login", String.class); String cookie = page.getHeaders().getFirst("Set-Cookie"); headers.set("Cookie", cookie); diff --git a/spring-boot-samples/spring-boot-sample-web-secure/src/test/java/sample/ui/secure/SampleSecureApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-secure/src/test/java/sample/ui/secure/SampleSecureApplicationTests.java index 53fdd641f4..2909fe9d95 100644 --- a/spring-boot-samples/spring-boot-sample-web-secure/src/test/java/sample/ui/secure/SampleSecureApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-secure/src/test/java/sample/ui/secure/SampleSecureApplicationTests.java @@ -21,7 +21,7 @@ import java.util.Arrays; import org.junit.Test; import org.junit.runner.RunWith; 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.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -52,7 +52,7 @@ public class SampleSecureApplicationTests { public void testHome() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); - ResponseEntity entity = RestTemplates.get().exchange( + ResponseEntity entity = new TestRestTemplate().exchange( "http://localhost:8080", HttpMethod.GET, new HttpEntity(headers), String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @@ -62,7 +62,7 @@ public class SampleSecureApplicationTests { @Test public void testCss() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080/css/bootstrap.min.css", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body")); diff --git a/spring-boot-samples/spring-boot-sample-web-static/src/test/java/sample/ui/SampleWebStaticApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-static/src/test/java/sample/ui/SampleWebStaticApplicationTests.java index 5435326f3f..8105354247 100644 --- a/spring-boot-samples/spring-boot-sample-web-static/src/test/java/sample/ui/SampleWebStaticApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-static/src/test/java/sample/ui/SampleWebStaticApplicationTests.java @@ -19,7 +19,7 @@ package sample.ui; import org.junit.Test; import org.junit.runner.RunWith; 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.http.HttpStatus; import org.springframework.http.MediaType; @@ -45,7 +45,7 @@ public class SampleWebStaticApplicationTests { @Test public void testHome() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity @@ -54,7 +54,7 @@ public class SampleWebStaticApplicationTests { @Test public void testCss() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080/webjars/bootstrap/3.0.3/css/bootstrap.min.css", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); diff --git a/spring-boot-samples/spring-boot-sample-web-ui/src/test/java/sample/ui/SampleWebUiApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-ui/src/test/java/sample/ui/SampleWebUiApplicationTests.java index e12c40974e..c93b73e2b7 100644 --- a/spring-boot-samples/spring-boot-sample-web-ui/src/test/java/sample/ui/SampleWebUiApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-ui/src/test/java/sample/ui/SampleWebUiApplicationTests.java @@ -21,7 +21,7 @@ import java.net.URI; import org.junit.Test; import org.junit.runner.RunWith; 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.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -49,7 +49,7 @@ public class SampleWebUiApplicationTests { @Test public void testHome() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity @@ -63,14 +63,15 @@ public class SampleWebUiApplicationTests { MultiValueMap map = new LinkedMultiValueMap(); map.set("text", "FOO text"); 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, location.toString().contains("localhost:8080")); } @Test public void testCss() throws Exception { - ResponseEntity entity = RestTemplates.get().getForEntity( + ResponseEntity entity = new TestRestTemplate().getForEntity( "http://localhost:8080/css/bootstrap.min.css", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body")); diff --git a/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java b/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java index 99bfbc3c39..9c0734d484 100644 --- a/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java +++ b/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java @@ -52,7 +52,7 @@ import org.springframework.web.context.support.GenericWebApplicationContext; *

* If you want to start a web server, mark the test class as * @WebAppConfiguration @IntegrationTest. 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 * @Autowired application context components into your test case to see the * internal effects of HTTP requests directly. *

diff --git a/spring-boot/src/main/java/org/springframework/boot/test/RestTemplates.java b/spring-boot/src/main/java/org/springframework/boot/test/TestRestTemplate.java similarity index 53% rename from spring-boot/src/main/java/org/springframework/boot/test/RestTemplates.java rename to spring-boot/src/main/java/org/springframework/boot/test/TestRestTemplate.java index 2a3f87f9b4..0d5d32f9dd 100644 --- a/spring-boot/src/main/java/org/springframework/boot/test/RestTemplates.java +++ b/spring-boot/src/main/java/org/springframework/boot/test/TestRestTemplate.java @@ -18,7 +18,7 @@ package org.springframework.boot.test; import java.io.IOException; import java.net.URI; -import java.util.ArrayList; +import java.util.Collections; import java.util.List; 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.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; +import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; @@ -39,62 +40,73 @@ import org.springframework.web.client.DefaultResponseErrorHandler; import org.springframework.web.client.RestTemplate; /** - * Convenient static factory for {@link RestTemplate} instances that are suitable for - * integration tests. They are fault tolerant, and optionally can carry Basic - * authentication headers. If Apache Http Client 4.3.2 or better is available - * (recommended) it will be used as the client, and configured to ignore cookies and - * redirects. + * Convenient subclass of {@link RestTemplate} that is suitable for integration tests. + * They are fault tolerant, and optionally can carry Basic authentication headers. If + * Apache Http Client 4.3.2 or better is available (recommended) it will be used as the + * client, and configured to ignore cookies and redirects. * * @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 - * cookies and does not throw exceptions on server side errors. - * @return a basic RestTemplate with no authentication + * Create a new {@link TestRestTemplate} instance. */ - public static RestTemplate get() { - return get(null, null); + public TestRestTemplate() { + this(null, null); } /** - * Factory method for a secure RestTemplate with Basic authentication that does not - * follow redirects, ignores cookies and does not throw exceptions on server side - * errors. - * @return a basic RestTemplate with Basic authentication + * Create a new {@link TestRestTemplate} instance with the specified credentials. + * @param username the username to use (or {@code null}) + * @param password the password (or {@code null}) */ - public static RestTemplate get(final String username, final String password) { - - List interceptors = new ArrayList(); - - 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)); + public TestRestTemplate(String username, String password) { + super(getRequestFactory(username, password)); 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 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 interceptors = Collections + . 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); + } }