Skip to content

Commit 07a50bb

Browse files
committed
Update the samples to make use of auto-configured TestRestTemplate
Closes spring-projectsgh-6730
1 parent 3c5cf02 commit 07a50bb

File tree

47 files changed

+375
-421
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+375
-421
lines changed

spring-boot-samples/spring-boot-sample-actuator-log4j2/src/test/java/sample/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.junit.Test;
2222
import org.junit.runner.RunWith;
2323

24-
import org.springframework.boot.context.embedded.LocalServerPort;
24+
import org.springframework.beans.factory.annotation.Autowired;
2525
import org.springframework.boot.test.context.SpringBootTest;
2626
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2727
import org.springframework.boot.test.web.client.TestRestTemplate;
@@ -42,14 +42,13 @@
4242
@DirtiesContext
4343
public class SampleActuatorLog4J2ApplicationTests {
4444

45-
@LocalServerPort
46-
private int port;
45+
@Autowired
46+
private TestRestTemplate restTemplate;
4747

4848
@Test
4949
public void testHome() throws Exception {
5050
@SuppressWarnings("rawtypes")
51-
ResponseEntity<Map> entity = new TestRestTemplate()
52-
.getForEntity("http://localhost:" + this.port, Map.class);
51+
ResponseEntity<Map> entity = this.restTemplate.getForEntity("/", Map.class);
5352
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
5453
@SuppressWarnings("unchecked")
5554
Map<String, Object> body = entity.getBody();

spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationTests.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.junit.Test;
2323
import org.junit.runner.RunWith;
2424

25-
import org.springframework.boot.context.embedded.LocalServerPort;
25+
import org.springframework.beans.factory.annotation.Autowired;
2626
import org.springframework.boot.test.context.SpringBootTest;
2727
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2828
import org.springframework.boot.test.web.client.TestRestTemplate;
@@ -47,43 +47,41 @@
4747
@DirtiesContext
4848
public class SampleActuatorUiApplicationTests {
4949

50-
@LocalServerPort
51-
private int port;
50+
@Autowired
51+
private TestRestTemplate restTemplate;
5252

5353
@Test
5454
public void testHome() throws Exception {
5555
HttpHeaders headers = new HttpHeaders();
5656
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
57-
ResponseEntity<String> entity = new TestRestTemplate().exchange(
58-
"http://localhost:" + this.port, HttpMethod.GET,
57+
ResponseEntity<String> entity = this.restTemplate.exchange("/", HttpMethod.GET,
5958
new HttpEntity<Void>(headers), String.class);
6059
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
6160
assertThat(entity.getBody()).contains("<title>Hello");
6261
}
6362

6463
@Test
6564
public void testCss() throws Exception {
66-
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
67-
"http://localhost:" + this.port + "/css/bootstrap.min.css", String.class);
65+
ResponseEntity<String> entity = this.restTemplate
66+
.getForEntity("/css/bootstrap.min.css", String.class);
6867
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
6968
assertThat(entity.getBody()).contains("body");
7069
}
7170

7271
@Test
7372
public void testMetrics() throws Exception {
7473
@SuppressWarnings("rawtypes")
75-
ResponseEntity<Map> entity = new TestRestTemplate()
76-
.getForEntity("http://localhost:" + this.port + "/metrics", Map.class);
74+
ResponseEntity<Map> entity = this.restTemplate.getForEntity("/metrics",
75+
Map.class);
7776
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
7877
}
7978

8079
@Test
8180
public void testError() throws Exception {
8281
HttpHeaders headers = new HttpHeaders();
8382
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
84-
ResponseEntity<String> entity = new TestRestTemplate().exchange(
85-
"http://localhost:" + this.port + "/error", HttpMethod.GET,
86-
new HttpEntity<Void>(headers), String.class);
83+
ResponseEntity<String> entity = this.restTemplate.exchange("/error",
84+
HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
8785
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
8886
assertThat(entity.getBody()).contains("<html>").contains("<body>")
8987
.contains("Please contact the operator with the above information");

spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/EndpointsPropertiesSampleActuatorApplicationTests.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import org.springframework.beans.factory.annotation.Autowired;
2525
import org.springframework.boot.autoconfigure.security.SecurityProperties;
26-
import org.springframework.boot.context.embedded.LocalServerPort;
2726
import org.springframework.boot.test.context.SpringBootTest;
2827
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2928
import org.springframework.boot.test.web.client.TestRestTemplate;
@@ -49,14 +48,14 @@ public class EndpointsPropertiesSampleActuatorApplicationTests {
4948
@Autowired
5049
private SecurityProperties security;
5150

52-
@LocalServerPort
53-
private int port;
51+
@Autowired
52+
private TestRestTemplate restTemplate;
5453

5554
@Test
5655
public void testCustomErrorPath() throws Exception {
5756
@SuppressWarnings("rawtypes")
58-
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
59-
.getForEntity("http://localhost:" + this.port + "/oops", Map.class);
57+
ResponseEntity<Map> entity = this.restTemplate
58+
.withBasicAuth("user", getPassword()).getForEntity("/oops", Map.class);
6059
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
6160
@SuppressWarnings("unchecked")
6261
Map<String, Object> body = entity.getBody();
@@ -66,9 +65,9 @@ public void testCustomErrorPath() throws Exception {
6665

6766
@Test
6867
public void testCustomContextPath() throws Exception {
69-
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
70-
.getForEntity("http://localhost:" + this.port + "/admin/health",
71-
String.class);
68+
ResponseEntity<String> entity = this.restTemplate
69+
.withBasicAuth("user", getPassword())
70+
.getForEntity("/admin/health", String.class);
7271
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
7372
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
7473
assertThat(entity.getBody()).contains("\"hello\":\"world\"");

spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/InsecureManagementSampleActuatorApplicationTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.junit.Test;
2222
import org.junit.runner.RunWith;
2323

24-
import org.springframework.boot.context.embedded.LocalServerPort;
24+
import org.springframework.beans.factory.annotation.Autowired;
2525
import org.springframework.boot.test.context.SpringBootTest;
2626
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2727
import org.springframework.boot.test.web.client.TestRestTemplate;
@@ -46,14 +46,13 @@
4646
@ActiveProfiles("unsecure-management")
4747
public class InsecureManagementSampleActuatorApplicationTests {
4848

49-
@LocalServerPort
50-
private int port;
49+
@Autowired
50+
private TestRestTemplate restTemplate;
5151

5252
@Test
5353
public void testHomeIsSecure() throws Exception {
5454
@SuppressWarnings("rawtypes")
55-
ResponseEntity<Map> entity = new TestRestTemplate()
56-
.getForEntity("http://localhost:" + this.port, Map.class);
55+
ResponseEntity<Map> entity = this.restTemplate.getForEntity("/", Map.class);
5756
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
5857
@SuppressWarnings("unchecked")
5958
Map<String, Object> body = entity.getBody();
@@ -70,8 +69,8 @@ public void testMetrics() throws Exception {
7069
// ignore;
7170
}
7271
@SuppressWarnings("rawtypes")
73-
ResponseEntity<Map> entity = new TestRestTemplate()
74-
.getForEntity("http://localhost:" + this.port + "/metrics", Map.class);
72+
ResponseEntity<Map> entity = this.restTemplate.getForEntity("/metrics",
73+
Map.class);
7574
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
7675
@SuppressWarnings("unchecked")
7776
Map<String, Object> body = entity.getBody();

spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/InsecureSampleActuatorApplicationTests.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.junit.Test;
2222
import org.junit.runner.RunWith;
2323

24-
import org.springframework.boot.context.embedded.LocalServerPort;
24+
import org.springframework.beans.factory.annotation.Autowired;
2525
import org.springframework.boot.test.context.SpringBootTest;
2626
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2727
import org.springframework.boot.test.web.client.TestRestTemplate;
@@ -44,14 +44,13 @@
4444
@DirtiesContext
4545
public class InsecureSampleActuatorApplicationTests {
4646

47-
@LocalServerPort
48-
private int port;
47+
@Autowired
48+
private TestRestTemplate restTemplate;
4949

5050
@Test
5151
public void testHome() throws Exception {
5252
@SuppressWarnings("rawtypes")
53-
ResponseEntity<Map> entity = new TestRestTemplate()
54-
.getForEntity("http://localhost:" + this.port, Map.class);
53+
ResponseEntity<Map> entity = this.restTemplate.getForEntity("/", Map.class);
5554
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
5655
@SuppressWarnings("unchecked")
5756
Map<String, Object> body = entity.getBody();

spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPathSampleActuatorApplicationTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.junit.Test;
2222
import org.junit.runner.RunWith;
2323

24-
import org.springframework.boot.context.embedded.LocalServerPort;
24+
import org.springframework.beans.factory.annotation.Autowired;
2525
import org.springframework.boot.test.context.SpringBootTest;
2626
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2727
import org.springframework.boot.test.web.client.TestRestTemplate;
@@ -43,22 +43,21 @@
4343
@DirtiesContext
4444
public class ManagementPathSampleActuatorApplicationTests {
4545

46-
@LocalServerPort
47-
private int port;
46+
@Autowired
47+
private TestRestTemplate restTemplate;
4848

4949
@Test
5050
public void testHealth() throws Exception {
51-
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
52-
"http://localhost:" + this.port + "/admin/health", String.class);
51+
ResponseEntity<String> entity = this.restTemplate.getForEntity("/admin/health",
52+
String.class);
5353
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
5454
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
5555
}
5656

5757
@Test
5858
public void testHomeIsSecure() throws Exception {
5959
@SuppressWarnings("rawtypes")
60-
ResponseEntity<Map> entity = new TestRestTemplate()
61-
.getForEntity("http://localhost:" + this.port, Map.class);
60+
ResponseEntity<Map> entity = this.restTemplate.getForEntity("/", Map.class);
6261
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
6362
@SuppressWarnings("unchecked")
6463
Map<String, Object> body = entity.getBody();

spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NoManagementSampleActuatorApplicationTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import org.springframework.beans.factory.annotation.Autowired;
2525
import org.springframework.boot.autoconfigure.security.SecurityProperties;
26-
import org.springframework.boot.context.embedded.LocalServerPort;
2726
import org.springframework.boot.test.context.SpringBootTest;
2827
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2928
import org.springframework.boot.test.web.client.TestRestTemplate;
@@ -48,14 +47,14 @@ public class NoManagementSampleActuatorApplicationTests {
4847
@Autowired
4948
private SecurityProperties security;
5049

51-
@LocalServerPort
52-
private int port = 0;
50+
@Autowired
51+
private TestRestTemplate restTemplate;
5352

5453
@Test
5554
public void testHome() throws Exception {
5655
@SuppressWarnings("rawtypes")
57-
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
58-
.getForEntity("http://localhost:" + this.port, Map.class);
56+
ResponseEntity<Map> entity = this.restTemplate
57+
.withBasicAuth("user", getPassword()).getForEntity("/", Map.class);
5958
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
6059
@SuppressWarnings("unchecked")
6160
Map<String, Object> body = entity.getBody();
@@ -66,8 +65,8 @@ public void testHome() throws Exception {
6665
public void testMetricsNotAvailable() throws Exception {
6766
testHome(); // makes sure some requests have been made
6867
@SuppressWarnings("rawtypes")
69-
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
70-
.getForEntity("http://localhost:" + this.port + "/metrics", Map.class);
68+
ResponseEntity<Map> entity = this.restTemplate
69+
.withBasicAuth("user", getPassword()).getForEntity("/metrics", Map.class);
7170
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
7271
}
7372

spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NonSensitiveHealthTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import org.junit.Test;
2020
import org.junit.runner.RunWith;
2121

22-
import org.springframework.boot.context.embedded.LocalServerPort;
22+
import org.springframework.beans.factory.annotation.Autowired;
2323
import org.springframework.boot.test.context.SpringBootTest;
2424
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2525
import org.springframework.boot.test.web.client.TestRestTemplate;
@@ -41,13 +41,13 @@
4141
@DirtiesContext
4242
public class NonSensitiveHealthTests {
4343

44-
@LocalServerPort
45-
private int port;
44+
@Autowired
45+
private TestRestTemplate restTemplate;
4646

4747
@Test
4848
public void testSecureHealth() throws Exception {
49-
ResponseEntity<String> entity = new TestRestTemplate()
50-
.getForEntity("http://localhost:" + this.port + "/health", String.class);
49+
ResponseEntity<String> entity = this.restTemplate.getForEntity("/health",
50+
String.class);
5151
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
5252
assertThat(entity.getBody()).doesNotContain("\"hello\":1");
5353
}

0 commit comments

Comments
 (0)