diff --git a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/test/java/sample/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/test/java/sample/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java index 55ad41ce205..72c73c34822 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/test/java/sample/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/test/java/sample/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java @@ -21,7 +21,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -42,14 +42,13 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleActuatorLog4J2ApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, Map.class); + ResponseEntity entity = this.restTemplate.getForEntity("/", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); @SuppressWarnings("unchecked") Map body = 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 e8b8de843dc..a72f62f86ca 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.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -47,15 +47,14 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleActuatorUiApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); - ResponseEntity entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port, HttpMethod.GET, + ResponseEntity entity = this.restTemplate.exchange("/", HttpMethod.GET, new HttpEntity(headers), String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("Hello"); @@ -63,8 +62,8 @@ public class SampleActuatorUiApplicationTests { @Test public void testCss() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate().getForEntity( - "http://localhost:" + this.port + "/css/bootstrap.min.css", String.class); + ResponseEntity<String> entity = this.restTemplate + .getForEntity("/css/bootstrap.min.css", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("body"); } @@ -72,8 +71,8 @@ public class SampleActuatorUiApplicationTests { @Test public void testMetrics() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port + "/metrics", Map.class); + ResponseEntity<Map> entity = this.restTemplate.getForEntity("/metrics", + Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); } @@ -81,9 +80,8 @@ public class SampleActuatorUiApplicationTests { public void testError() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); - ResponseEntity<String> entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port + "/error", HttpMethod.GET, - new HttpEntity<Void>(headers), String.class); + ResponseEntity<String> entity = this.restTemplate.exchange("/error", + HttpMethod.GET, new HttpEntity<Void>(headers), String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); assertThat(entity.getBody()).contains("<html>").contains("<body>") .contains("Please contact the operator with the above information"); 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 e17c49f366a..4a4863d454d 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 @@ -23,7 +23,6 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.security.SecurityProperties; -import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -49,14 +48,14 @@ public class EndpointsPropertiesSampleActuatorApplicationTests { @Autowired private SecurityProperties security; - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testCustomErrorPath() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:" + this.port + "/oops", Map.class); + ResponseEntity<Map> entity = this.restTemplate + .withBasicAuth("user", getPassword()).getForEntity("/oops", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); @@ -66,9 +65,9 @@ public class EndpointsPropertiesSampleActuatorApplicationTests { @Test public void testCustomContextPath() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:" + this.port + "/admin/health", - String.class); + ResponseEntity<String> entity = this.restTemplate + .withBasicAuth("user", getPassword()) + .getForEntity("/admin/health", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("\"status\":\"UP\""); assertThat(entity.getBody()).contains("\"hello\":\"world\""); diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/InsecureManagementSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/InsecureManagementSampleActuatorApplicationTests.java index ef9d0bc3e90..9e5a6208bb7 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/InsecureManagementSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/InsecureManagementSampleActuatorApplicationTests.java @@ -21,7 +21,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -46,14 +46,13 @@ import static org.assertj.core.api.Assertions.assertThat; @ActiveProfiles("unsecure-management") public class InsecureManagementSampleActuatorApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHomeIsSecure() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, Map.class); + ResponseEntity<Map> entity = this.restTemplate.getForEntity("/", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); @@ -70,8 +69,8 @@ public class InsecureManagementSampleActuatorApplicationTests { // ignore; } @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port + "/metrics", Map.class); + ResponseEntity<Map> entity = this.restTemplate.getForEntity("/metrics", + Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/InsecureSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/InsecureSampleActuatorApplicationTests.java index 7b4d5375a37..aba7cc15d41 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/InsecureSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/InsecureSampleActuatorApplicationTests.java @@ -21,7 +21,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -44,14 +44,13 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class InsecureSampleActuatorApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, Map.class); + ResponseEntity<Map> entity = this.restTemplate.getForEntity("/", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPathSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPathSampleActuatorApplicationTests.java index 5e154c16d36..c7db62b77f3 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPathSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPathSampleActuatorApplicationTests.java @@ -21,7 +21,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -43,13 +43,13 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class ManagementPathSampleActuatorApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHealth() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate().getForEntity( - "http://localhost:" + this.port + "/admin/health", String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/admin/health", + String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("\"status\":\"UP\""); } @@ -57,8 +57,7 @@ public class ManagementPathSampleActuatorApplicationTests { @Test public void testHomeIsSecure() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, Map.class); + ResponseEntity<Map> entity = this.restTemplate.getForEntity("/", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); 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 9533c837df9..6e63518c8f4 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,7 +23,6 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.security.SecurityProperties; -import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -48,14 +47,14 @@ public class NoManagementSampleActuatorApplicationTests { @Autowired private SecurityProperties security; - @LocalServerPort - private int port = 0; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:" + this.port, Map.class); + ResponseEntity<Map> entity = this.restTemplate + .withBasicAuth("user", getPassword()).getForEntity("/", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); @@ -66,8 +65,8 @@ public class NoManagementSampleActuatorApplicationTests { public void testMetricsNotAvailable() throws Exception { testHome(); // makes sure some requests have been made @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:" + this.port + "/metrics", Map.class); + ResponseEntity<Map> entity = this.restTemplate + .withBasicAuth("user", getPassword()).getForEntity("/metrics", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); } diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NonSensitiveHealthTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NonSensitiveHealthTests.java index 0b095cb868a..e17fb524b2a 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NonSensitiveHealthTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NonSensitiveHealthTests.java @@ -19,7 +19,7 @@ package sample.actuator; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -41,13 +41,13 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class NonSensitiveHealthTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testSecureHealth() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port + "/health", String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/health", + String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).doesNotContain("\"hello\":1"); } 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 d159aa465a3..a0016284349 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 @@ -26,7 +26,6 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties; -import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -55,14 +54,13 @@ public class SampleActuatorApplicationTests { @Autowired private SecurityProperties security; - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHomeIsSecure() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, Map.class); + ResponseEntity<Map> entity = this.restTemplate.getForEntity("/", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); @@ -73,25 +71,22 @@ public class SampleActuatorApplicationTests { @Test public void testMetricsIsSecure() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port + "/metrics", Map.class); + ResponseEntity<Map> entity = this.restTemplate.getForEntity("/metrics", + Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); - entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port + "/metrics/", Map.class); + entity = this.restTemplate.getForEntity("/metrics/", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); - entity = new TestRestTemplate().getForEntity( - "http://localhost:" + this.port + "/metrics/foo", Map.class); + entity = this.restTemplate.getForEntity("/metrics/foo", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); - entity = new TestRestTemplate().getForEntity( - "http://localhost:" + this.port + "/metrics.json", Map.class); + entity = this.restTemplate.getForEntity("/metrics.json", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); } @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:" + this.port, Map.class); + ResponseEntity<Map> entity = this.restTemplate + .withBasicAuth("user", getPassword()).getForEntity("/", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); @@ -102,8 +97,8 @@ public class SampleActuatorApplicationTests { public void testMetrics() throws Exception { testHome(); // makes sure some requests have been made @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:" + this.port + "/metrics", Map.class); + ResponseEntity<Map> entity = this.restTemplate + .withBasicAuth("user", getPassword()).getForEntity("/metrics", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); @@ -113,8 +108,8 @@ public class SampleActuatorApplicationTests { @Test public void testEnv() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:" + this.port + "/env", Map.class); + ResponseEntity<Map> entity = this.restTemplate + .withBasicAuth("user", getPassword()).getForEntity("/env", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); @@ -123,8 +118,8 @@ public class SampleActuatorApplicationTests { @Test public void testHealth() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port + "/health", String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/health", + String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("\"status\":\"UP\""); assertThat(entity.getBody()).doesNotContain("\"hello\":\"1\""); @@ -132,16 +127,17 @@ public class SampleActuatorApplicationTests { @Test public void testSecureHealth() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:" + this.port + "/health", String.class); + ResponseEntity<String> entity = this.restTemplate + .withBasicAuth("user", getPassword()) + .getForEntity("/health", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("\"hello\":1"); } @Test public void testInfo() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port + "/info", String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/info", + String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()) .contains("\"artifact\":\"spring-boot-sample-actuator\""); @@ -154,8 +150,8 @@ public class SampleActuatorApplicationTests { @Test public void testErrorPage() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:" + this.port + "/foo", String.class); + ResponseEntity<String> entity = this.restTemplate + .withBasicAuth("user", getPassword()).getForEntity("/foo", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); String body = entity.getBody(); assertThat(body).contains("\"error\":"); @@ -166,9 +162,9 @@ public class SampleActuatorApplicationTests { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); HttpEntity<?> request = new HttpEntity<Void>(headers); - ResponseEntity<String> entity = new TestRestTemplate("user", getPassword()) - .exchange("http://localhost:" + this.port + "/foo", HttpMethod.GET, - request, String.class); + ResponseEntity<String> entity = this.restTemplate + .withBasicAuth("user", getPassword()) + .exchange("/foo", HttpMethod.GET, request, String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); String body = entity.getBody(); assertThat(body).as("Body was null").isNotNull(); @@ -177,11 +173,10 @@ public class SampleActuatorApplicationTests { @Test public void testTrace() throws Exception { - new TestRestTemplate().getForEntity("http://localhost:" + this.port + "/health", - String.class); + this.restTemplate.getForEntity("/health", String.class); @SuppressWarnings("rawtypes") - ResponseEntity<List> entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:" + this.port + "/trace", List.class); + ResponseEntity<List> entity = this.restTemplate + .withBasicAuth("user", getPassword()).getForEntity("/trace", List.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); @SuppressWarnings("unchecked") List<Map<String, Object>> list = entity.getBody(); @@ -195,8 +190,7 @@ public class SampleActuatorApplicationTests { @Test public void testErrorPageDirectAccess() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port + "/error", Map.class); + ResponseEntity<Map> entity = this.restTemplate.getForEntity("/error", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); @@ -208,8 +202,8 @@ public class SampleActuatorApplicationTests { @SuppressWarnings("unchecked") public void testBeans() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<List> entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:" + this.port + "/beans", List.class); + ResponseEntity<List> entity = this.restTemplate + .withBasicAuth("user", getPassword()).getForEntity("/beans", List.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).hasSize(1); Map<String, Object> body = (Map<String, Object>) entity.getBody().get(0); @@ -219,9 +213,9 @@ public class SampleActuatorApplicationTests { @Test public void testConfigProps() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:" + this.port + "/configprops", - Map.class); + ResponseEntity<Map> entity = this.restTemplate + .withBasicAuth("user", getPassword()) + .getForEntity("/configprops", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ServletPathInsecureSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ServletPathInsecureSampleActuatorApplicationTests.java index 84e577f79ef..5cd8aff6e10 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ServletPathInsecureSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ServletPathInsecureSampleActuatorApplicationTests.java @@ -21,7 +21,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -44,14 +44,14 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class ServletPathInsecureSampleActuatorApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port + "/spring/", Map.class); + ResponseEntity<Map> entity = this.restTemplate.getForEntity("/spring/", + Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); @@ -62,8 +62,8 @@ public class ServletPathInsecureSampleActuatorApplicationTests { @Test public void testMetricsIsSecure() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate().getForEntity( - "http://localhost:" + this.port + "/spring/metrics", Map.class); + ResponseEntity<Map> entity = this.restTemplate.getForEntity("/spring/metrics", + Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); } diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ServletPathSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ServletPathSampleActuatorApplicationTests.java index f5cca8d05b2..301d25fd86f 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ServletPathSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ServletPathSampleActuatorApplicationTests.java @@ -21,7 +21,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -43,15 +43,14 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class ServletPathSampleActuatorApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testErrorPath() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate("user", "password") - .getForEntity("http://localhost:" + this.port + "/spring/error", - Map.class); + ResponseEntity<Map> entity = this.restTemplate.getForEntity("/spring/error", + Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); @@ -61,8 +60,8 @@ public class ServletPathSampleActuatorApplicationTests { @Test public void testHealth() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate().getForEntity( - "http://localhost:" + this.port + "/spring/health", String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/spring/health", + String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("\"status\":\"UP\""); } @@ -70,8 +69,8 @@ public class ServletPathSampleActuatorApplicationTests { @Test public void testHomeIsSecure() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port + "/spring/", Map.class); + ResponseEntity<Map> entity = this.restTemplate.getForEntity("/spring/", + Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); 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 3bc2284bbae..b512888173b 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,6 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.security.SecurityProperties; -import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -47,14 +46,14 @@ public class ShutdownSampleActuatorApplicationTests { @Autowired private SecurityProperties security; - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:" + this.port, Map.class); + ResponseEntity<Map> entity = this.restTemplate + .withBasicAuth("user", getPassword()).getForEntity("/", Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); @@ -64,9 +63,9 @@ public class ShutdownSampleActuatorApplicationTests { @Test public void testShutdown() throws Exception { @SuppressWarnings("rawtypes") - ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) - .postForEntity("http://localhost:" + this.port + "/shutdown", null, - Map.class); + ResponseEntity<Map> entity = this.restTemplate + .withBasicAuth("user", getPassword()) + .postForEntity("/shutdown", null, Map.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody(); diff --git a/spring-boot-samples/spring-boot-sample-hateoas/src/test/java/sample/hateoas/SampleHateoasApplicationTests.java b/spring-boot-samples/spring-boot-sample-hateoas/src/test/java/sample/hateoas/SampleHateoasApplicationTests.java index 1deb82339aa..1b0df644c21 100644 --- a/spring-boot-samples/spring-boot-sample-hateoas/src/test/java/sample/hateoas/SampleHateoasApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-hateoas/src/test/java/sample/hateoas/SampleHateoasApplicationTests.java @@ -16,20 +16,18 @@ package sample.hateoas; -import java.net.URI; - import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; -import org.springframework.http.RequestEntity; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; @@ -39,13 +37,13 @@ import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class SampleHateoasApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void hasHalLinks() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate().getForEntity( - "http://localhost:" + this.port + "/customers/1", String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/customers/1", + String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).startsWith( "{\"id\":1,\"firstName\":\"Oliver\"" + ",\"lastName\":\"Gierke\""); @@ -56,10 +54,9 @@ public class SampleHateoasApplicationTests { public void producesJsonWhenXmlIsPreferred() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.set(HttpHeaders.ACCEPT, "application/xml;q=0.9,application/json;q=0.8"); - RequestEntity<?> request = new RequestEntity<Void>(headers, HttpMethod.GET, - URI.create("http://localhost:" + this.port + "/customers/1")); - ResponseEntity<String> response = new TestRestTemplate().exchange(request, - String.class); + HttpEntity<?> request = new HttpEntity<>(headers); + ResponseEntity<String> response = this.restTemplate.exchange("/customers/1", + HttpMethod.GET, request, String.class); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(response.getHeaders().getContentType()) .isEqualTo(MediaType.parseMediaType("application/json;charset=UTF-8")); diff --git a/spring-boot-samples/spring-boot-sample-hypermedia-ui/src/test/java/sample/hypermedia/ui/SampleHypermediaUiApplicationTests.java b/spring-boot-samples/spring-boot-sample-hypermedia-ui/src/test/java/sample/hypermedia/ui/SampleHypermediaUiApplicationTests.java index 3a589248ef9..84dd325a744 100644 --- a/spring-boot-samples/spring-boot-sample-hypermedia-ui/src/test/java/sample/hypermedia/ui/SampleHypermediaUiApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-hypermedia-ui/src/test/java/sample/hypermedia/ui/SampleHypermediaUiApplicationTests.java @@ -16,20 +16,19 @@ package sample.hypermedia.ui; -import java.net.URI; import java.util.Arrays; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; -import org.springframework.http.RequestEntity; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; @@ -40,20 +39,18 @@ import static org.assertj.core.api.Assertions.assertThat; "management.context-path=" }) public class SampleHypermediaUiApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void home() { - String response = new TestRestTemplate() - .getForObject("http://localhost:" + this.port, String.class); + String response = this.restTemplate.getForObject("/", String.class); assertThat(response).contains("Hello World"); } @Test public void links() { - String response = new TestRestTemplate().getForObject( - "http://localhost:" + this.port + "/actuator", String.class); + String response = this.restTemplate.getForObject("/actuator", String.class); assertThat(response).contains("\"_links\":"); } @@ -61,10 +58,8 @@ public class SampleHypermediaUiApplicationTests { public void linksWithJson() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); - ResponseEntity<String> response = new TestRestTemplate().exchange( - new RequestEntity<Void>(headers, HttpMethod.GET, - new URI("http://localhost:" + this.port + "/actuator")), - String.class); + ResponseEntity<String> response = this.restTemplate.exchange("/actuator", + HttpMethod.GET, new HttpEntity<Void>(headers), String.class); assertThat(response.getBody()).contains("\"_links\":"); } @@ -72,9 +67,8 @@ public class SampleHypermediaUiApplicationTests { public void homeWithHtml() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); - ResponseEntity<String> response = new TestRestTemplate() - .exchange(new RequestEntity<Void>(headers, HttpMethod.GET, - new URI("http://localhost:" + this.port)), String.class); + ResponseEntity<String> response = this.restTemplate.exchange("/", HttpMethod.GET, + new HttpEntity<Void>(headers), String.class); assertThat(response.getBody()).contains("Hello World"); } diff --git a/spring-boot-samples/spring-boot-sample-hypermedia/src/test/java/sample/hypermedia/SampleHypermediaApplicationHomePageTests.java b/spring-boot-samples/spring-boot-sample-hypermedia/src/test/java/sample/hypermedia/SampleHypermediaApplicationHomePageTests.java index d546c4d8a89..1c8395a3fb1 100644 --- a/spring-boot-samples/spring-boot-sample-hypermedia/src/test/java/sample/hypermedia/SampleHypermediaApplicationHomePageTests.java +++ b/spring-boot-samples/spring-boot-sample-hypermedia/src/test/java/sample/hypermedia/SampleHypermediaApplicationHomePageTests.java @@ -16,20 +16,19 @@ package sample.hypermedia; -import java.net.URI; import java.util.Arrays; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; -import org.springframework.http.RequestEntity; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; @@ -39,13 +38,12 @@ import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class SampleHypermediaApplicationHomePageTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void home() { - String response = new TestRestTemplate() - .getForObject("http://localhost:" + this.port, String.class); + String response = this.restTemplate.getForObject("/", String.class); assertThat(response).contains("404"); } @@ -53,10 +51,8 @@ public class SampleHypermediaApplicationHomePageTests { public void linksWithJson() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); - ResponseEntity<String> response = new TestRestTemplate().exchange( - new RequestEntity<Void>(headers, HttpMethod.GET, - new URI("http://localhost:" + this.port + "/actuator")), - String.class); + ResponseEntity<String> response = this.restTemplate.exchange("/actuator", + HttpMethod.GET, new HttpEntity<Void>(headers), String.class); assertThat(response.getBody()).contains("\"_links\":"); } @@ -64,10 +60,8 @@ public class SampleHypermediaApplicationHomePageTests { public void halWithHtml() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); - ResponseEntity<String> response = new TestRestTemplate().exchange( - new RequestEntity<Void>(headers, HttpMethod.GET, - new URI("http://localhost:" + this.port + "/actuator/")), - String.class); + ResponseEntity<String> response = this.restTemplate.exchange("/actuator/", + HttpMethod.GET, new HttpEntity<Void>(headers), String.class); assertThat(response.getBody()).contains("HAL Browser"); } diff --git a/spring-boot-samples/spring-boot-sample-jersey/src/test/java/sample/jersey/SampleJerseyApplicationTests.java b/spring-boot-samples/spring-boot-sample-jersey/src/test/java/sample/jersey/SampleJerseyApplicationTests.java index 56231576e97..b140f14514a 100644 --- a/spring-boot-samples/spring-boot-sample-jersey/src/test/java/sample/jersey/SampleJerseyApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-jersey/src/test/java/sample/jersey/SampleJerseyApplicationTests.java @@ -19,7 +19,7 @@ package sample.jersey; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -33,30 +33,28 @@ import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class SampleJerseyApplicationTests { - @LocalServerPort - private int port; - - private TestRestTemplate restTemplate = new TestRestTemplate(); + @Autowired + private TestRestTemplate restTemplate; @Test public void contextLoads() { - ResponseEntity<String> entity = this.restTemplate - .getForEntity("http://localhost:" + this.port + "/hello", String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/hello", + String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); } @Test public void reverse() { - ResponseEntity<String> entity = this.restTemplate.getForEntity( - "http://localhost:" + this.port + "/reverse?input=olleh", String.class); + ResponseEntity<String> entity = this.restTemplate + .getForEntity("/reverse?input=olleh", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).isEqualTo("hello"); } @Test public void validation() { - ResponseEntity<String> entity = this.restTemplate - .getForEntity("http://localhost:" + this.port + "/reverse", String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/reverse", + String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); } diff --git a/spring-boot-samples/spring-boot-sample-jersey1/src/test/java/sample/jersey1/SampleJersey1ApplicationTests.java b/spring-boot-samples/spring-boot-sample-jersey1/src/test/java/sample/jersey1/SampleJersey1ApplicationTests.java index ef754a92189..033070fd971 100644 --- a/spring-boot-samples/spring-boot-sample-jersey1/src/test/java/sample/jersey1/SampleJersey1ApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-jersey1/src/test/java/sample/jersey1/SampleJersey1ApplicationTests.java @@ -19,7 +19,7 @@ package sample.jersey1; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -31,14 +31,13 @@ import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class SampleJersey1ApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test - public void contextLoads() { - assertThat(new TestRestTemplate() - .getForObject("http://localhost:" + this.port + "/", String.class)) - .isEqualTo("Hello World"); + public void rootReturnsHelloWorld() { + assertThat(this.restTemplate.getForObject("/", String.class)) + .isEqualTo("Hello World"); } } diff --git a/spring-boot-samples/spring-boot-sample-jetty-jsp/src/test/java/sample/jetty/jsp/SampleWebJspApplicationTests.java b/spring-boot-samples/spring-boot-sample-jetty-jsp/src/test/java/sample/jetty/jsp/SampleWebJspApplicationTests.java index 013231731ab..eaaf9ee248b 100644 --- a/spring-boot-samples/spring-boot-sample-jetty-jsp/src/test/java/sample/jetty/jsp/SampleWebJspApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-jetty-jsp/src/test/java/sample/jetty/jsp/SampleWebJspApplicationTests.java @@ -19,7 +19,7 @@ package sample.jetty.jsp; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleWebJspApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testJspWithEl() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("/resources/text.txt"); } 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 997cb5b60bc..3a2ea9b7ef6 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 @@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -49,13 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleJettyApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).isEqualTo("Hello World"); } @@ -66,11 +65,8 @@ public class SampleJettyApplicationTests { requestHeaders.set("Accept-Encoding", "gzip"); HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders); - TestRestTemplate restTemplate = new TestRestTemplate(); - - ResponseEntity<byte[]> entity = restTemplate.exchange( - "http://localhost:" + this.port, HttpMethod.GET, requestEntity, - byte[].class); + ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET, + requestEntity, byte[].class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); diff --git a/spring-boot-samples/spring-boot-sample-jetty8-ssl/src/test/java/sample/jetty8/ssl/SampleJetty8SslApplicationTests.java b/spring-boot-samples/spring-boot-sample-jetty8-ssl/src/test/java/sample/jetty8/ssl/SampleJetty8SslApplicationTests.java index 59d9e457f38..60420899cc7 100644 --- a/spring-boot-samples/spring-boot-sample-jetty8-ssl/src/test/java/sample/jetty8/ssl/SampleJetty8SslApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-jetty8-ssl/src/test/java/sample/jetty8/ssl/SampleJetty8SslApplicationTests.java @@ -19,11 +19,11 @@ package sample.jetty8.ssl; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; @@ -41,14 +41,15 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleJetty8SslApplicationTests { + @Autowired + private TestRestTemplate restTemplate; + @LocalServerPort private int port; @Test public void testHome() throws Exception { - TestRestTemplate testRestTemplate = new TestRestTemplate(HttpClientOption.SSL); - ResponseEntity<String> entity = testRestTemplate - .getForEntity("https://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).isEqualTo("Hello World"); } diff --git a/spring-boot-samples/spring-boot-sample-jetty8/src/test/java/sample/jetty8/SampleJetty8ApplicationTests.java b/spring-boot-samples/spring-boot-sample-jetty8/src/test/java/sample/jetty8/SampleJetty8ApplicationTests.java index 00fa720f4c8..5165011db85 100644 --- a/spring-boot-samples/spring-boot-sample-jetty8/src/test/java/sample/jetty8/SampleJetty8ApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-jetty8/src/test/java/sample/jetty8/SampleJetty8ApplicationTests.java @@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -49,13 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleJetty8ApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).isEqualTo("Hello World"); } @@ -65,10 +64,8 @@ public class SampleJetty8ApplicationTests { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("Accept-Encoding", "gzip"); HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders); - TestRestTemplate restTemplate = new TestRestTemplate(); - ResponseEntity<byte[]> entity = restTemplate.exchange( - "http://localhost:" + this.port, HttpMethod.GET, requestEntity, - byte[].class); + ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET, + requestEntity, byte[].class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); GZIPInputStream inflater = new GZIPInputStream( new ByteArrayInputStream(entity.getBody())); diff --git a/spring-boot-samples/spring-boot-sample-jetty92/src/test/java/sample/jetty92/SampleJetty92ApplicationTests.java b/spring-boot-samples/spring-boot-sample-jetty92/src/test/java/sample/jetty92/SampleJetty92ApplicationTests.java index 2ba84b99401..622317654b0 100644 --- a/spring-boot-samples/spring-boot-sample-jetty92/src/test/java/sample/jetty92/SampleJetty92ApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-jetty92/src/test/java/sample/jetty92/SampleJetty92ApplicationTests.java @@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -49,13 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleJetty92ApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).isEqualTo("Hello World"); } @@ -65,10 +64,8 @@ public class SampleJetty92ApplicationTests { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("Accept-Encoding", "gzip"); HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders); - TestRestTemplate restTemplate = new TestRestTemplate(); - ResponseEntity<byte[]> entity = restTemplate.exchange( - "http://localhost:" + this.port, HttpMethod.GET, requestEntity, - byte[].class); + ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET, + requestEntity, byte[].class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); GZIPInputStream inflater = new GZIPInputStream( new ByteArrayInputStream(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 baeb2fadabe..b36fc2653aa 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,6 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.security.SecurityProperties; -import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -42,23 +41,22 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleServletApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Autowired private SecurityProperties security; @Test public void testHomeIsSecure() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); } @Test public void testHome() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate("user", getPassword()) - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate + .withBasicAuth("user", getPassword()).getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).isEqualTo("Hello World"); } diff --git a/spring-boot-samples/spring-boot-sample-testng/src/test/java/sample/testng/SampleTestNGApplicationTests.java b/spring-boot-samples/spring-boot-sample-testng/src/test/java/sample/testng/SampleTestNGApplicationTests.java index 3a297d7689f..d1bef9f11c4 100644 --- a/spring-boot-samples/spring-boot-sample-testng/src/test/java/sample/testng/SampleTestNGApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-testng/src/test/java/sample/testng/SampleTestNGApplicationTests.java @@ -18,7 +18,7 @@ package sample.testng; import org.testng.annotations.Test; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -38,13 +38,12 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleTestNGApplicationTests extends AbstractTestNGSpringContextTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).isEqualTo("Hello World"); } diff --git a/spring-boot-samples/spring-boot-sample-tomcat-jsp/src/test/java/sample/tomcat/jsp/SampleWebJspApplicationTests.java b/spring-boot-samples/spring-boot-sample-tomcat-jsp/src/test/java/sample/tomcat/jsp/SampleWebJspApplicationTests.java index e57595671c6..2548ef07ccc 100644 --- a/spring-boot-samples/spring-boot-sample-tomcat-jsp/src/test/java/sample/tomcat/jsp/SampleWebJspApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-tomcat-jsp/src/test/java/sample/tomcat/jsp/SampleWebJspApplicationTests.java @@ -19,7 +19,7 @@ package sample.tomcat.jsp; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleWebJspApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testJspWithEl() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("/resources/text.txt"); } diff --git a/spring-boot-samples/spring-boot-sample-tomcat-ssl/src/test/java/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java b/spring-boot-samples/spring-boot-sample-tomcat-ssl/src/test/java/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java index f7b42c68a6f..81856affbbc 100644 --- a/spring-boot-samples/spring-boot-sample-tomcat-ssl/src/test/java/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-tomcat-ssl/src/test/java/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java @@ -19,11 +19,10 @@ package sample.tomcat.ssl; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; @@ -36,14 +35,12 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleTomcatSslApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { - TestRestTemplate testRestTemplate = new TestRestTemplate(HttpClientOption.SSL); - ResponseEntity<String> entity = testRestTemplate - .getForEntity("https://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).isEqualTo("Hello, world"); } 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 e98d93b4b2b..2ec1de04674 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 @@ -21,6 +21,7 @@ import org.junit.runner.RunWith; import sample.tomcat.service.HelloWorldService; import sample.tomcat.web.SampleController; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; @@ -28,7 +29,6 @@ import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoCo import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; -import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -52,8 +52,8 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class NonAutoConfigurationSampleTomcatApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Configuration @Import({ EmbeddedServletContainerAutoConfiguration.class, @@ -73,8 +73,7 @@ public class NonAutoConfigurationSampleTomcatApplicationTests { @Test public void testHome() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).isEqualTo("Hello World"); } 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 6a6f7db6c84..bc397cff323 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 @@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -49,13 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleTomcatApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).isEqualTo("Hello World"); } @@ -65,10 +64,8 @@ public class SampleTomcatApplicationTests { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("Accept-Encoding", "gzip"); HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders); - TestRestTemplate restTemplate = new TestRestTemplate(); - ResponseEntity<byte[]> entity = restTemplate.exchange( - "http://localhost:" + this.port, HttpMethod.GET, requestEntity, - byte[].class); + ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET, + requestEntity, byte[].class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); GZIPInputStream inflater = new GZIPInputStream( new ByteArrayInputStream(entity.getBody())); diff --git a/spring-boot-samples/spring-boot-sample-tomcat7-jsp/src/test/java/sample/tomcat7/jsp/SampleWebJspApplicationTests.java b/spring-boot-samples/spring-boot-sample-tomcat7-jsp/src/test/java/sample/tomcat7/jsp/SampleWebJspApplicationTests.java index 7a74cf7d5e9..a112afcde76 100644 --- a/spring-boot-samples/spring-boot-sample-tomcat7-jsp/src/test/java/sample/tomcat7/jsp/SampleWebJspApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-tomcat7-jsp/src/test/java/sample/tomcat7/jsp/SampleWebJspApplicationTests.java @@ -19,7 +19,7 @@ package sample.tomcat7.jsp; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleWebJspApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testJspWithEl() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("/resources/text.txt"); } diff --git a/spring-boot-samples/spring-boot-sample-tomcat7-ssl/src/test/java/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java b/spring-boot-samples/spring-boot-sample-tomcat7-ssl/src/test/java/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java index f7b42c68a6f..81856affbbc 100644 --- a/spring-boot-samples/spring-boot-sample-tomcat7-ssl/src/test/java/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-tomcat7-ssl/src/test/java/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java @@ -19,11 +19,10 @@ package sample.tomcat.ssl; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; @@ -36,14 +35,12 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleTomcatSslApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { - TestRestTemplate testRestTemplate = new TestRestTemplate(HttpClientOption.SSL); - ResponseEntity<String> entity = testRestTemplate - .getForEntity("https://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).isEqualTo("Hello, world"); } diff --git a/spring-boot-samples/spring-boot-sample-tomcat80-ssl/src/test/java/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java b/spring-boot-samples/spring-boot-sample-tomcat80-ssl/src/test/java/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java index f7b42c68a6f..81856affbbc 100644 --- a/spring-boot-samples/spring-boot-sample-tomcat80-ssl/src/test/java/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-tomcat80-ssl/src/test/java/sample/tomcat/ssl/SampleTomcatSslApplicationTests.java @@ -19,11 +19,10 @@ package sample.tomcat.ssl; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; @@ -36,14 +35,12 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleTomcatSslApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { - TestRestTemplate testRestTemplate = new TestRestTemplate(HttpClientOption.SSL); - ResponseEntity<String> entity = testRestTemplate - .getForEntity("https://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).isEqualTo("Hello, world"); } 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 87fe7977a1f..734a008878a 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.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleTraditionalApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHomeJsp() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); String body = entity.getBody(); assertThat(body).contains("<html>").contains("<h1>Home</h1>"); @@ -54,8 +53,8 @@ public class SampleTraditionalApplicationTests { @Test public void testStaticPage() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate().getForEntity( - "http://localhost:" + this.port + "/index.html", String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/index.html", + String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); String body = entity.getBody(); assertThat(body).contains("<html>").contains("<h1>Hello</h1>"); diff --git a/spring-boot-samples/spring-boot-sample-undertow-ssl/src/test/java/sample/undertow/ssl/SampleUndertowSslApplicationTests.java b/spring-boot-samples/spring-boot-sample-undertow-ssl/src/test/java/sample/undertow/ssl/SampleUndertowSslApplicationTests.java index 43184217209..4fafb17e436 100644 --- a/spring-boot-samples/spring-boot-sample-undertow-ssl/src/test/java/sample/undertow/ssl/SampleUndertowSslApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-undertow-ssl/src/test/java/sample/undertow/ssl/SampleUndertowSslApplicationTests.java @@ -19,11 +19,10 @@ package sample.undertow.ssl; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; @@ -41,14 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleUndertowSslApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { - TestRestTemplate testRestTemplate = new TestRestTemplate(HttpClientOption.SSL); - ResponseEntity<String> entity = testRestTemplate - .getForEntity("https://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).isEqualTo("Hello World"); } diff --git a/spring-boot-samples/spring-boot-sample-undertow/src/test/java/sample/undertow/SampleUndertowApplicationTests.java b/spring-boot-samples/spring-boot-sample-undertow/src/test/java/sample/undertow/SampleUndertowApplicationTests.java index ba3f71bc404..05ee9d4d39a 100644 --- a/spring-boot-samples/spring-boot-sample-undertow/src/test/java/sample/undertow/SampleUndertowApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-undertow/src/test/java/sample/undertow/SampleUndertowApplicationTests.java @@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -49,8 +49,8 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleUndertowApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { @@ -67,10 +67,8 @@ public class SampleUndertowApplicationTests { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("Accept-Encoding", "gzip"); HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders); - TestRestTemplate restTemplate = new TestRestTemplate(); - ResponseEntity<byte[]> entity = restTemplate.exchange( - "http://localhost:" + this.port, HttpMethod.GET, requestEntity, - byte[].class); + ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET, + requestEntity, byte[].class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); GZIPInputStream inflater = new GZIPInputStream( new ByteArrayInputStream(entity.getBody())); @@ -84,8 +82,8 @@ public class SampleUndertowApplicationTests { } private void assertOkResponse(String path, String body) { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port + path, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity(path, + String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).isEqualTo(body); } diff --git a/spring-boot-samples/spring-boot-sample-web-freemarker/src/test/java/sample/freemarker/SampleWebFreeMarkerApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-freemarker/src/test/java/sample/freemarker/SampleWebFreeMarkerApplicationTests.java index f6b435de875..1adec7ff61e 100644 --- a/spring-boot-samples/spring-boot-sample-web-freemarker/src/test/java/sample/freemarker/SampleWebFreeMarkerApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-freemarker/src/test/java/sample/freemarker/SampleWebFreeMarkerApplicationTests.java @@ -21,7 +21,7 @@ import java.util.Arrays; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -47,13 +47,13 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleWebFreeMarkerApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate testRestTemplate; @Test public void testFreeMarkerTemplate() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.testRestTemplate.getForEntity("/", + String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("Hello, Andy"); } @@ -64,9 +64,8 @@ public class SampleWebFreeMarkerApplicationTests { headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); HttpEntity<String> requestEntity = new HttpEntity<String>(headers); - ResponseEntity<String> responseEntity = new TestRestTemplate().exchange( - "http://localhost:" + this.port + "/does-not-exist", HttpMethod.GET, - requestEntity, String.class); + ResponseEntity<String> responseEntity = this.testRestTemplate + .exchange("/does-not-exist", HttpMethod.GET, requestEntity, String.class); assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); assertThat(responseEntity.getBody()) diff --git a/spring-boot-samples/spring-boot-sample-web-groovy-templates/src/test/java/sample/groovytemplates/SampleGroovyTemplateApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-groovy-templates/src/test/java/sample/groovytemplates/SampleGroovyTemplateApplicationTests.java index 938fdc28235..108f13c16f0 100644 --- a/spring-boot-samples/spring-boot-sample-web-groovy-templates/src/test/java/sample/groovytemplates/SampleGroovyTemplateApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-groovy-templates/src/test/java/sample/groovytemplates/SampleGroovyTemplateApplicationTests.java @@ -21,6 +21,7 @@ import java.net.URI; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -47,10 +48,12 @@ public class SampleGroovyTemplateApplicationTests { @LocalServerPort private int port; + @Autowired + private TestRestTemplate restTemplate; + @Test public void testHome() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("<title>Messages"); assertThat(entity.getBody()).doesNotContain("layout:fragment"); @@ -61,15 +64,14 @@ public class SampleGroovyTemplateApplicationTests { MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); map.set("text", "FOO text"); map.set("summary", "FOO"); - URI location = new TestRestTemplate() - .postForLocation("http://localhost:" + this.port, map); + URI location = this.restTemplate.postForLocation("/", map); assertThat(location.toString()).contains("localhost:" + this.port); } @Test public void testCss() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate().getForEntity( - "http://localhost:" + this.port + "/css/bootstrap.min.css", String.class); + ResponseEntity<String> entity = this.restTemplate + .getForEntity("/css/bootstrap.min.css", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("body"); } 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 13aa8b62686..824e18fcde1 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.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleWebJspApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testJspWithEl() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("/resources/text.txt"); } diff --git a/spring-boot-samples/spring-boot-sample-web-method-security/src/test/java/sample/security/method/SampleMethodSecurityApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-method-security/src/test/java/sample/security/method/SampleMethodSecurityApplicationTests.java index 431c6737ccc..fa23d465674 100644 --- a/spring-boot-samples/spring-boot-sample-web-method-security/src/test/java/sample/security/method/SampleMethodSecurityApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-method-security/src/test/java/sample/security/method/SampleMethodSecurityApplicationTests.java @@ -23,6 +23,7 @@ import java.util.regex.Pattern; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -33,6 +34,7 @@ import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.http.client.support.BasicAuthorizationInterceptor; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.util.LinkedMultiValueMap; @@ -53,12 +55,14 @@ public class SampleMethodSecurityApplicationTests { @LocalServerPort private int port; + @Autowired + private TestRestTemplate restTemplate; + @Test public void testHome() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); - ResponseEntity<String> entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port, HttpMethod.GET, + ResponseEntity<String> entity = this.restTemplate.exchange("/", HttpMethod.GET, new HttpEntity<Void>(headers), String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("<title>Login"); @@ -72,8 +76,8 @@ public class SampleMethodSecurityApplicationTests { form.set("username", "admin"); form.set("password", "admin"); getCsrf(form, headers); - ResponseEntity<String> entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port + "/login", HttpMethod.POST, + ResponseEntity<String> entity = this.restTemplate.exchange("/login", + HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(form, headers), String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); @@ -89,14 +93,14 @@ public class SampleMethodSecurityApplicationTests { form.set("username", "user"); form.set("password", "user"); getCsrf(form, headers); - ResponseEntity<String> entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port + "/login", HttpMethod.POST, + ResponseEntity<String> entity = this.restTemplate.exchange("/login", + HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(form, headers), String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); String cookie = entity.getHeaders().getFirst("Set-Cookie"); headers.set("Cookie", cookie); - ResponseEntity<String> page = new TestRestTemplate().exchange( + ResponseEntity<String> page = this.restTemplate.exchange( entity.getHeaders().getLocation(), HttpMethod.GET, new HttpEntity<Void>(headers), String.class); assertThat(page.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN); @@ -105,28 +109,46 @@ public class SampleMethodSecurityApplicationTests { @Test public void testManagementProtected() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port + "/beans", String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/beans", + String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); } @Test public void testManagementAuthorizedAccess() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate("admin", "admin") - .getForEntity("http://localhost:" + this.port + "/beans", String.class); - assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); + BasicAuthorizationInterceptor basicAuthInterceptor = new BasicAuthorizationInterceptor( + "admin", "admin"); + this.restTemplate.getRestTemplate().getInterceptors().add(basicAuthInterceptor); + try { + ResponseEntity<String> entity = this.restTemplate.getForEntity("/beans", + String.class); + assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); + } + finally { + this.restTemplate.getRestTemplate().getInterceptors() + .remove(basicAuthInterceptor); + } } @Test public void testManagementUnauthorizedAccess() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate("user", "user") - .getForEntity("http://localhost:" + this.port + "/beans", String.class); - assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN); + BasicAuthorizationInterceptor basicAuthInterceptor = new BasicAuthorizationInterceptor( + "user", "user"); + this.restTemplate.getRestTemplate().getInterceptors().add(basicAuthInterceptor); + try { + ResponseEntity<String> entity = this.restTemplate.getForEntity("/beans", + String.class); + assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN); + } + finally { + this.restTemplate.getRestTemplate().getInterceptors() + .remove(basicAuthInterceptor); + } } private void getCsrf(MultiValueMap<String, String> form, HttpHeaders headers) { - ResponseEntity<String> page = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port + "/login", String.class); + ResponseEntity<String> page = this.restTemplate.getForEntity("/login", + String.class); String cookie = page.getHeaders().getFirst("Set-Cookie"); headers.set("Cookie", cookie); String body = page.getBody(); diff --git a/spring-boot-samples/spring-boot-sample-web-mustache/src/test/java/sample/mustache/SampleWebMustacheApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-mustache/src/test/java/sample/mustache/SampleWebMustacheApplicationTests.java index 122c9a02687..397b2324814 100644 --- a/spring-boot-samples/spring-boot-sample-web-mustache/src/test/java/sample/mustache/SampleWebMustacheApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-mustache/src/test/java/sample/mustache/SampleWebMustacheApplicationTests.java @@ -21,7 +21,7 @@ import java.util.Arrays; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -47,13 +47,12 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleWebMustacheApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testMustacheTemplate() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("Hello, Andy"); } @@ -63,9 +62,8 @@ public class SampleWebMustacheApplicationTests { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); HttpEntity<String> requestEntity = new HttpEntity<String>(headers); - ResponseEntity<String> responseEntity = new TestRestTemplate().exchange( - "http://localhost:" + this.port + "/does-not-exist", HttpMethod.GET, - requestEntity, String.class); + ResponseEntity<String> responseEntity = this.restTemplate + .exchange("/does-not-exist", HttpMethod.GET, requestEntity, String.class); assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); assertThat(responseEntity.getBody()) .contains("Something went wrong: 404 Not Found"); @@ -76,9 +74,8 @@ public class SampleWebMustacheApplicationTests { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); HttpEntity<String> requestEntity = new HttpEntity<String>(headers); - ResponseEntity<String> entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port + "/serviceUnavailable", HttpMethod.GET, - requestEntity, String.class); + ResponseEntity<String> entity = this.restTemplate.exchange("/serviceUnavailable", + HttpMethod.GET, requestEntity, String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE); assertThat(entity.getBody()).contains("I'm a 503"); } @@ -88,9 +85,8 @@ public class SampleWebMustacheApplicationTests { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); HttpEntity<String> requestEntity = new HttpEntity<String>(headers); - ResponseEntity<String> entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port + "/bang", HttpMethod.GET, requestEntity, - String.class); + ResponseEntity<String> entity = this.restTemplate.exchange("/bang", + HttpMethod.GET, requestEntity, String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); assertThat(entity.getBody()).contains("I'm a 5xx"); } @@ -100,9 +96,8 @@ public class SampleWebMustacheApplicationTests { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); HttpEntity<String> requestEntity = new HttpEntity<String>(headers); - ResponseEntity<String> entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port + "/insufficientStorage", HttpMethod.GET, - requestEntity, String.class); + ResponseEntity<String> entity = this.restTemplate.exchange("/insufficientStorage", + HttpMethod.GET, requestEntity, String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INSUFFICIENT_STORAGE); assertThat(entity.getBody()).contains("I'm a 507"); } diff --git a/spring-boot-samples/spring-boot-sample-web-secure-custom/src/test/java/sample/web/secure/custom/SampleWebSecureCustomApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-secure-custom/src/test/java/sample/web/secure/custom/SampleWebSecureCustomApplicationTests.java index 13a8d3b7ff0..70f83cc19ca 100644 --- a/spring-boot-samples/spring-boot-sample-web-secure-custom/src/test/java/sample/web/secure/custom/SampleWebSecureCustomApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-secure-custom/src/test/java/sample/web/secure/custom/SampleWebSecureCustomApplicationTests.java @@ -23,6 +23,7 @@ import java.util.regex.Pattern; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -50,6 +51,9 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleWebSecureCustomApplicationTests { + @Autowired + private TestRestTemplate restTemplate; + @LocalServerPort private int port; @@ -57,8 +61,7 @@ public class SampleWebSecureCustomApplicationTests { public void testHome() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); - ResponseEntity<String> entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port, HttpMethod.GET, + ResponseEntity<String> entity = this.restTemplate.exchange("/", HttpMethod.GET, new HttpEntity<Void>(headers), String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); assertThat(entity.getHeaders().getLocation().toString()) @@ -69,9 +72,8 @@ public class SampleWebSecureCustomApplicationTests { public void testLoginPage() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); - ResponseEntity<String> entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port + "/login", HttpMethod.GET, - new HttpEntity<Void>(headers), String.class); + ResponseEntity<String> entity = this.restTemplate.exchange("/login", + HttpMethod.GET, new HttpEntity<Void>(headers), String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("_csrf"); } @@ -84,8 +86,8 @@ public class SampleWebSecureCustomApplicationTests { MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>(); form.set("username", "user"); form.set("password", "user"); - ResponseEntity<String> entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port + "/login", HttpMethod.POST, + ResponseEntity<String> entity = this.restTemplate.exchange("/login", + HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(form, headers), String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); @@ -96,8 +98,8 @@ public class SampleWebSecureCustomApplicationTests { private HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); - ResponseEntity<String> page = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port + "/login", String.class); + ResponseEntity<String> page = this.restTemplate.getForEntity("/login", + String.class); assertThat(page.getStatusCode()).isEqualTo(HttpStatus.OK); String cookie = page.getHeaders().getFirst("Set-Cookie"); headers.set("Cookie", cookie); @@ -110,8 +112,8 @@ public class SampleWebSecureCustomApplicationTests { @Test public void testCss() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate().getForEntity( - "http://localhost:" + this.port + "/css/bootstrap.min.css", String.class); + ResponseEntity<String> entity = this.restTemplate + .getForEntity("/css/bootstrap.min.css", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("body"); } diff --git a/spring-boot-samples/spring-boot-sample-web-secure-github/src/test/java/sample/web/secure/github/SampleGithubApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-secure-github/src/test/java/sample/web/secure/github/SampleGithubApplicationTests.java index 30f840622ea..bd0bb89a7a4 100644 --- a/spring-boot-samples/spring-boot-sample-web-secure-github/src/test/java/sample/web/secure/github/SampleGithubApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-secure-github/src/test/java/sample/web/secure/github/SampleGithubApplicationTests.java @@ -21,6 +21,7 @@ import java.net.URI; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -46,11 +47,12 @@ public class SampleGithubApplicationTests { @LocalServerPort private int port; + @Autowired + private TestRestTemplate restTemplate; + @Test public void everythingIsSecuredByDefault() throws Exception { - TestRestTemplate restTemplate = new TestRestTemplate(); - ResponseEntity<Void> entity = restTemplate - .getForEntity("http://localhost:" + this.port, Void.class); + ResponseEntity<Void> entity = this.restTemplate.getForEntity("/", Void.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); assertThat(entity.getHeaders().getLocation()) .isEqualTo(URI.create("http://localhost:" + this.port + "/login")); @@ -58,9 +60,8 @@ public class SampleGithubApplicationTests { @Test public void loginRedirectsToGithub() throws Exception { - TestRestTemplate restTemplate = new TestRestTemplate(); - ResponseEntity<Void> entity = restTemplate - .getForEntity("http://localhost:" + this.port + "/login", Void.class); + ResponseEntity<Void> entity = this.restTemplate.getForEntity("/login", + Void.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); assertThat(entity.getHeaders().getLocation().toString()) .startsWith("https://github.com/login/oauth"); diff --git a/spring-boot-samples/spring-boot-sample-web-secure-jdbc/src/test/java/sample/web/secure/jdbc/SampleWebSecureJdbcApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-secure-jdbc/src/test/java/sample/web/secure/jdbc/SampleWebSecureJdbcApplicationTests.java index 05559562142..644b9815ee8 100644 --- a/spring-boot-samples/spring-boot-sample-web-secure-jdbc/src/test/java/sample/web/secure/jdbc/SampleWebSecureJdbcApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-secure-jdbc/src/test/java/sample/web/secure/jdbc/SampleWebSecureJdbcApplicationTests.java @@ -23,6 +23,7 @@ import java.util.regex.Pattern; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -50,6 +51,9 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleWebSecureJdbcApplicationTests { + @Autowired + private TestRestTemplate restTemplate; + @LocalServerPort private int port; @@ -57,8 +61,7 @@ public class SampleWebSecureJdbcApplicationTests { public void testHome() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); - ResponseEntity<String> entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port, HttpMethod.GET, + ResponseEntity<String> entity = this.restTemplate.exchange("/", HttpMethod.GET, new HttpEntity<Void>(headers), String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); assertThat(entity.getHeaders().getLocation().toString()) @@ -69,9 +72,8 @@ public class SampleWebSecureJdbcApplicationTests { public void testLoginPage() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); - ResponseEntity<String> entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port + "/login", HttpMethod.GET, - new HttpEntity<Void>(headers), String.class); + ResponseEntity<String> entity = this.restTemplate.exchange("/login", + HttpMethod.GET, new HttpEntity<Void>(headers), String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("_csrf"); } @@ -84,8 +86,8 @@ public class SampleWebSecureJdbcApplicationTests { MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>(); form.set("username", "user"); form.set("password", "user"); - ResponseEntity<String> entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port + "/login", HttpMethod.POST, + ResponseEntity<String> entity = this.restTemplate.exchange("/login", + HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(form, headers), String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); @@ -96,8 +98,8 @@ public class SampleWebSecureJdbcApplicationTests { private HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); - ResponseEntity<String> page = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port + "/login", String.class); + ResponseEntity<String> page = this.restTemplate.getForEntity("/login", + String.class); assertThat(page.getStatusCode()).isEqualTo(HttpStatus.OK); String cookie = page.getHeaders().getFirst("Set-Cookie"); headers.set("Cookie", cookie); @@ -110,8 +112,8 @@ public class SampleWebSecureJdbcApplicationTests { @Test public void testCss() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate().getForEntity( - "http://localhost:" + this.port + "/css/bootstrap.min.css", String.class); + ResponseEntity<String> entity = this.restTemplate + .getForEntity("/css/bootstrap.min.css", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("body"); } diff --git a/spring-boot-samples/spring-boot-sample-web-secure/src/test/java/sample/web/secure/SampleSecureApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-secure/src/test/java/sample/web/secure/SampleSecureApplicationTests.java index e8a488c5214..098e28f7819 100644 --- a/spring-boot-samples/spring-boot-sample-web-secure/src/test/java/sample/web/secure/SampleSecureApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-secure/src/test/java/sample/web/secure/SampleSecureApplicationTests.java @@ -23,6 +23,7 @@ import java.util.regex.Pattern; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -50,6 +51,9 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleSecureApplicationTests { + @Autowired + private TestRestTemplate restTemplate; + @LocalServerPort private int port; @@ -57,8 +61,7 @@ public class SampleSecureApplicationTests { public void testHome() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); - ResponseEntity<String> entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port, HttpMethod.GET, + ResponseEntity<String> entity = this.restTemplate.exchange("/", HttpMethod.GET, new HttpEntity<Void>(headers), String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); assertThat(entity.getHeaders().getLocation().toString()) @@ -69,9 +72,8 @@ public class SampleSecureApplicationTests { public void testLoginPage() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); - ResponseEntity<String> entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port + "/login", HttpMethod.GET, - new HttpEntity<Void>(headers), String.class); + ResponseEntity<String> entity = this.restTemplate.exchange("/login", + HttpMethod.GET, new HttpEntity<Void>(headers), String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("_csrf"); } @@ -84,8 +86,8 @@ public class SampleSecureApplicationTests { MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>(); form.set("username", "user"); form.set("password", "user"); - ResponseEntity<String> entity = new TestRestTemplate().exchange( - "http://localhost:" + this.port + "/login", HttpMethod.POST, + ResponseEntity<String> entity = this.restTemplate.exchange("/login", + HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(form, headers), String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); @@ -96,8 +98,8 @@ public class SampleSecureApplicationTests { private HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); - ResponseEntity<String> page = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port + "/login", String.class); + ResponseEntity<String> page = this.restTemplate.getForEntity("/login", + String.class); assertThat(page.getStatusCode()).isEqualTo(HttpStatus.OK); String cookie = page.getHeaders().getFirst("Set-Cookie"); headers.set("Cookie", cookie); @@ -110,8 +112,8 @@ public class SampleSecureApplicationTests { @Test public void testCss() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate().getForEntity( - "http://localhost:" + this.port + "/css/bootstrap.min.css", String.class); + ResponseEntity<String> entity = this.restTemplate + .getForEntity("/css/bootstrap.min.css", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("body"); } diff --git a/spring-boot-samples/spring-boot-sample-web-static/src/test/java/sample/web/staticcontent/SampleWebStaticApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-static/src/test/java/sample/web/staticcontent/SampleWebStaticApplicationTests.java index 3e6ad7fb552..480aa50524c 100644 --- a/spring-boot-samples/spring-boot-sample-web-static/src/test/java/sample/web/staticcontent/SampleWebStaticApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-static/src/test/java/sample/web/staticcontent/SampleWebStaticApplicationTests.java @@ -19,7 +19,7 @@ package sample.web.staticcontent; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -41,24 +41,20 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleWebStaticApplicationTests { - @LocalServerPort - private int port = 0; + @Autowired + private TestRestTemplate restTemplate; @Test public void testHome() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("<title>Static"); } @Test public void testCss() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity( - "http://localhost:" + this.port - + "/webjars/bootstrap/3.0.3/css/bootstrap.min.css", - String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity( + "/webjars/bootstrap/3.0.3/css/bootstrap.min.css", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("body"); assertThat(entity.getHeaders().getContentType()) diff --git a/spring-boot-samples/spring-boot-sample-web-thymeleaf3/src/test/java/sample/web/thymeleaf3/SampleWebThymeleaf3ApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-thymeleaf3/src/test/java/sample/web/thymeleaf3/SampleWebThymeleaf3ApplicationTests.java index 536e97fa871..29542289802 100644 --- a/spring-boot-samples/spring-boot-sample-web-thymeleaf3/src/test/java/sample/web/thymeleaf3/SampleWebThymeleaf3ApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-thymeleaf3/src/test/java/sample/web/thymeleaf3/SampleWebThymeleaf3ApplicationTests.java @@ -21,6 +21,7 @@ import java.net.URI; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -44,13 +45,15 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleWebThymeleaf3ApplicationTests { + @Autowired + private TestRestTemplate restTemplate; + @LocalServerPort private int port; @Test public void testHome() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("<title>Messages"); assertThat(entity.getBody()).doesNotContain("layout:fragment"); @@ -61,15 +64,14 @@ public class SampleWebThymeleaf3ApplicationTests { MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); map.set("text", "FOO text"); map.set("summary", "FOO"); - URI location = new TestRestTemplate() - .postForLocation("http://localhost:" + this.port, map); + URI location = this.restTemplate.postForLocation("/", map); assertThat(location.toString()).contains("localhost:" + this.port); } @Test public void testCss() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate().getForEntity( - "http://localhost:" + this.port + "/css/bootstrap.min.css", String.class); + ResponseEntity<String> entity = this.restTemplate + .getForEntity("/css/bootstrap.min.css", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("body"); } diff --git a/spring-boot-samples/spring-boot-sample-web-ui/src/test/java/sample/web/ui/SampleWebUiApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-ui/src/test/java/sample/web/ui/SampleWebUiApplicationTests.java index dca42c6da86..57c81c8bf45 100644 --- a/spring-boot-samples/spring-boot-sample-web-ui/src/test/java/sample/web/ui/SampleWebUiApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-ui/src/test/java/sample/web/ui/SampleWebUiApplicationTests.java @@ -21,6 +21,7 @@ import java.net.URI; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -44,13 +45,15 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleWebUiApplicationTests { + @Autowired + private TestRestTemplate restTemplate; + @LocalServerPort private int port; @Test public void testHome() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("<title>Messages"); assertThat(entity.getBody()).doesNotContain("layout:fragment"); @@ -61,14 +64,13 @@ public class SampleWebUiApplicationTests { MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); map.set("text", "FOO text"); map.set("summary", "FOO"); - URI location = new TestRestTemplate() - .postForLocation("http://localhost:" + this.port, map); + URI location = this.restTemplate.postForLocation("/", map); assertThat(location.toString()).contains("localhost:" + this.port); } @Test public void testCss() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate().getForEntity( + ResponseEntity<String> entity = this.restTemplate.getForEntity( "http://localhost:" + this.port + "/css/bootstrap.min.css", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("body"); diff --git a/spring-boot-samples/spring-boot-sample-web-velocity/src/test/java/sample/web/velocity/SampleWebVelocityApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-velocity/src/test/java/sample/web/velocity/SampleWebVelocityApplicationTests.java index e75e4fa3402..c7c76f243ba 100644 --- a/spring-boot-samples/spring-boot-sample-web-velocity/src/test/java/sample/web/velocity/SampleWebVelocityApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-velocity/src/test/java/sample/web/velocity/SampleWebVelocityApplicationTests.java @@ -21,7 +21,7 @@ import java.util.Arrays; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; @@ -47,13 +47,12 @@ import static org.assertj.core.api.Assertions.assertThat; @DirtiesContext public class SampleWebVelocityApplicationTests { - @LocalServerPort - private int port; + @Autowired + private TestRestTemplate restTemplate; @Test public void testVelocityTemplate() throws Exception { - ResponseEntity<String> entity = new TestRestTemplate() - .getForEntity("http://localhost:" + this.port, String.class); + ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("Hello, Andy"); } @@ -64,9 +63,8 @@ public class SampleWebVelocityApplicationTests { headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); HttpEntity<String> requestEntity = new HttpEntity<String>(headers); - ResponseEntity<String> responseEntity = new TestRestTemplate().exchange( - "http://localhost:" + this.port + "/does-not-exist", HttpMethod.GET, - requestEntity, String.class); + ResponseEntity<String> responseEntity = this.restTemplate + .exchange("/does-not-exist", HttpMethod.GET, requestEntity, String.class); assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); assertThat(responseEntity.getBody())