Update the samples to make use of auto-configured TestRestTemplate

Closes gh-6730
pull/6746/merge
Andy Wilkinson 8 years ago
parent 3c5cf02882
commit 07a50bb16c

@ -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<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();

@ -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<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>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");

@ -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\"");

@ -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();

@ -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();

@ -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();

@ -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);
}

@ -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");
}

@ -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();

@ -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);
}

@ -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();

@ -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();

@ -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"));

@ -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");
}

@ -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");
}

@ -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);
}

@ -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");
}
}

@ -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");
}

@ -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);

@ -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");
}

@ -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()));

@ -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()));

@ -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");
}

@ -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");
}

@ -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");
}

@ -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");
}

@ -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");
}

@ -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()));

@ -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");
}

@ -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");
}

@ -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");
}

@ -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>");

@ -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");
}

@ -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);
}

@ -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())

@ -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");
}

@ -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");
}

@ -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();

@ -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");
}

@ -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");
}

@ -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");

@ -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");
}

@ -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");
}

@ -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())

@ -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");
}

@ -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");

@ -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())

Loading…
Cancel
Save