Skip to content

Commit 1cc1fc6

Browse files
committed
Use AssertJ in spring-boot-samples
See gh-5083
1 parent 962a598 commit 1cc1fc6

File tree

97 files changed

+580
-733
lines changed

Some content is hidden

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

97 files changed

+580
-733
lines changed

spring-boot-samples/spring-boot-sample-activemq/src/test/java/sample/activemq/SampleActiveMqTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.springframework.boot.test.SpringApplicationConfiguration;
2828
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2929

30-
import static org.junit.Assert.assertTrue;
30+
import static org.assertj.core.api.Assertions.assertThat;
3131

3232
/**
3333
* Integration tests for demo application.
@@ -48,7 +48,7 @@ public class SampleActiveMqTests {
4848
public void sendSimpleMessage() throws InterruptedException, JMSException {
4949
this.producer.send("Test message");
5050
Thread.sleep(1000L);
51-
assertTrue(this.outputCapture.toString().contains("Test message"));
51+
assertThat(this.outputCapture.toString().contains("Test message")).isTrue();
5252
}
5353

5454
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.springframework.test.annotation.DirtiesContext;
3131
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3232

33-
import static org.junit.Assert.assertEquals;
33+
import static org.assertj.core.api.Assertions.assertThat;
3434

3535
/**
3636
* Basic integration tests for service demo application.
@@ -51,10 +51,10 @@ public void testHome() throws Exception {
5151
@SuppressWarnings("rawtypes")
5252
ResponseEntity<Map> entity = new TestRestTemplate()
5353
.getForEntity("http://localhost:" + port, Map.class);
54-
assertEquals(HttpStatus.OK, entity.getStatusCode());
54+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
5555
@SuppressWarnings("unchecked")
5656
Map<String, Object> body = entity.getBody();
57-
assertEquals("Hello Daniel", body.get("message"));
57+
assertThat(body.get("message")).isEqualTo("Hello Daniel");
5858
}
5959

6060
}

spring-boot-samples/spring-boot-sample-actuator-noweb/src/test/java/sample/actuator/noweb/SampleActuatorNoWebApplicationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.test.annotation.DirtiesContext;
2626
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2727

28-
import static org.junit.Assert.assertNotNull;
28+
import static org.assertj.core.api.Assertions.assertThat;
2929

3030
/**
3131
* Basic integration tests for service demo application.
@@ -42,7 +42,7 @@ public class SampleActuatorNoWebApplicationTests {
4242

4343
@Test
4444
public void endpointsExist() throws Exception {
45-
assertNotNull(this.endpoint);
45+
assertThat(this.endpoint).isNotNull();
4646
}
4747

4848
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.springframework.test.annotation.DirtiesContext;
3131
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3232

33-
import static org.junit.Assert.assertEquals;
33+
import static org.assertj.core.api.Assertions.assertThat;
3434

3535
/**
3636
* Integration tests for separate management and main service ports.
@@ -53,23 +53,23 @@ public class SampleActuatorUiApplicationPortTests {
5353
public void testHome() throws Exception {
5454
ResponseEntity<String> entity = new TestRestTemplate()
5555
.getForEntity("http://localhost:" + this.port, String.class);
56-
assertEquals(HttpStatus.OK, entity.getStatusCode());
56+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
5757
}
5858

5959
@Test
6060
public void testMetrics() throws Exception {
6161
@SuppressWarnings("rawtypes")
6262
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
6363
"http://localhost:" + this.managementPort + "/metrics", Map.class);
64-
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
64+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
6565
}
6666

6767
@Test
6868
public void testHealth() throws Exception {
6969
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
7070
"http://localhost:" + this.managementPort + "/health", String.class);
71-
assertEquals(HttpStatus.OK, entity.getStatusCode());
72-
assertEquals("{\"status\":\"UP\"}", entity.getBody());
71+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
72+
assertThat(entity.getBody()).isEqualTo("{\"status\":\"UP\"}");
7373
}
7474

7575
}

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

+9-15
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
import org.springframework.test.annotation.DirtiesContext;
3636
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3737

38-
import static org.junit.Assert.assertEquals;
39-
import static org.junit.Assert.assertTrue;
38+
import static org.assertj.core.api.Assertions.assertThat;
4039

4140
/**
4241
* Basic integration tests for demo application.
@@ -59,25 +58,24 @@ public void testHome() throws Exception {
5958
ResponseEntity<String> entity = new TestRestTemplate().exchange(
6059
"http://localhost:" + this.port, HttpMethod.GET,
6160
new HttpEntity<Void>(headers), String.class);
62-
assertEquals(HttpStatus.OK, entity.getStatusCode());
63-
assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(),
64-
entity.getBody().contains("<title>Hello"));
61+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
62+
assertThat(entity.getBody()).contains("<title>Hello");
6563
}
6664

6765
@Test
6866
public void testCss() throws Exception {
6967
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
7068
"http://localhost:" + this.port + "/css/bootstrap.min.css", String.class);
71-
assertEquals(HttpStatus.OK, entity.getStatusCode());
72-
assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body"));
69+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
70+
assertThat(entity.getBody()).contains("body");
7371
}
7472

7573
@Test
7674
public void testMetrics() throws Exception {
7775
@SuppressWarnings("rawtypes")
7876
ResponseEntity<Map> entity = new TestRestTemplate()
7977
.getForEntity("http://localhost:" + this.port + "/metrics", Map.class);
80-
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
78+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
8179
}
8280

8381
@Test
@@ -87,13 +85,9 @@ public void testError() throws Exception {
8785
ResponseEntity<String> entity = new TestRestTemplate().exchange(
8886
"http://localhost:" + this.port + "/error", HttpMethod.GET,
8987
new HttpEntity<Void>(headers), String.class);
90-
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
91-
assertTrue("Wrong body:\n" + entity.getBody(),
92-
entity.getBody().contains("<html>"));
93-
assertTrue("Wrong body:\n" + entity.getBody(),
94-
entity.getBody().contains("<body>"));
95-
assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody()
96-
.contains("Please contact the operator with the above information"));
88+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
89+
assertThat(entity.getBody()).contains("<html>").contains("<body>")
90+
.contains("Please contact the operator with the above information");
9791
}
9892

9993
}

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

+7-11
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
import org.springframework.test.context.ActiveProfiles;
3434
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3535

36-
import static org.junit.Assert.assertEquals;
37-
import static org.junit.Assert.assertTrue;
36+
import static org.assertj.core.api.Assertions.assertThat;
3837

3938
/**
4039
* Integration tests for endpoints configuration.
@@ -59,24 +58,21 @@ public void testCustomErrorPath() throws Exception {
5958
@SuppressWarnings("rawtypes")
6059
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
6160
.getForEntity("http://localhost:" + this.port + "/oops", Map.class);
62-
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
61+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
6362
@SuppressWarnings("unchecked")
6463
Map<String, Object> body = entity.getBody();
65-
assertEquals("None", body.get("error"));
66-
assertEquals(999, body.get("status"));
64+
assertThat(body.get("error")).isEqualTo("None");
65+
assertThat(body.get("status")).isEqualTo(999);
6766
}
6867

6968
@Test
7069
public void testCustomContextPath() throws Exception {
7170
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
7271
.getForEntity("http://localhost:" + this.port + "/admin/health",
7372
String.class);
74-
assertEquals(HttpStatus.OK, entity.getStatusCode());
75-
assertTrue("Wrong body: " + entity.getBody(),
76-
entity.getBody().contains("\"status\":\"UP\""));
77-
System.err.println(entity.getBody());
78-
assertTrue("Wrong body: " + entity.getBody(),
79-
entity.getBody().contains("\"hello\":\"world\""));
73+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
74+
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
75+
assertThat(entity.getBody()).contains("\"hello\":\"world\"");
8076
}
8177

8278
private String getPassword() {

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

+8-11
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
import org.springframework.test.annotation.DirtiesContext;
3333
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3434

35-
import static org.junit.Assert.assertEquals;
36-
import static org.junit.Assert.assertTrue;
35+
import static org.assertj.core.api.Assertions.assertThat;
3736

3837
/**
3938
* Integration tests for separate management and main service ports.
@@ -61,10 +60,10 @@ public void testHome() throws Exception {
6160
@SuppressWarnings("rawtypes")
6261
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
6362
.getForEntity("http://localhost:" + this.port, Map.class);
64-
assertEquals(HttpStatus.OK, entity.getStatusCode());
63+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
6564
@SuppressWarnings("unchecked")
6665
Map<String, Object> body = entity.getBody();
67-
assertEquals("Hello Phil", body.get("message"));
66+
assertThat(body.get("message")).isEqualTo("Hello Phil");
6867
}
6968

7069
@Test
@@ -73,27 +72,25 @@ public void testMetrics() throws Exception {
7372
@SuppressWarnings("rawtypes")
7473
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
7574
"http://localhost:" + this.managementPort + "/admin/metrics", Map.class);
76-
assertEquals(HttpStatus.OK, entity.getStatusCode());
75+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
7776
}
7877

7978
@Test
8079
public void testHealth() throws Exception {
8180
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
8281
"http://localhost:" + this.managementPort + "/admin/health",
8382
String.class);
84-
assertEquals(HttpStatus.OK, entity.getStatusCode());
85-
assertTrue("Wrong body: " + entity.getBody(),
86-
entity.getBody().contains("\"status\":\"UP\""));
83+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
84+
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
8785
}
8886

8987
@Test
9088
public void testMissing() throws Exception {
9189
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
9290
"http://localhost:" + this.managementPort + "/admin/missing",
9391
String.class);
94-
assertEquals(HttpStatus.NOT_FOUND, entity.getStatusCode());
95-
assertTrue("Wrong body: " + entity.getBody(),
96-
entity.getBody().contains("\"status\":404"));
92+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
93+
assertThat(entity.getBody()).contains("\"status\":404");
9794
}
9895

9996
private String getPassword() {

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

+6-10
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
import org.springframework.test.context.ActiveProfiles;
3232
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3333

34-
import static org.junit.Assert.assertEquals;
35-
import static org.junit.Assert.assertFalse;
36-
import static org.junit.Assert.assertTrue;
34+
import static org.assertj.core.api.Assertions.assertThat;
3735

3836
/**
3937
* Integration tests for insecured service endpoints (even with Spring Security on
@@ -56,12 +54,11 @@ public void testHomeIsSecure() throws Exception {
5654
@SuppressWarnings("rawtypes")
5755
ResponseEntity<Map> entity = new TestRestTemplate()
5856
.getForEntity("http://localhost:" + this.port, Map.class);
59-
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
57+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
6058
@SuppressWarnings("unchecked")
6159
Map<String, Object> body = entity.getBody();
62-
assertEquals("Wrong body: " + body, "Unauthorized", body.get("error"));
63-
assertFalse("Wrong headers: " + entity.getHeaders(),
64-
entity.getHeaders().containsKey("Set-Cookie"));
60+
assertThat(body.get("error")).isEqualTo("Unauthorized");
61+
assertThat(entity.getHeaders()).doesNotContainKey("Set-Cookie");
6562
}
6663

6764
@Test
@@ -75,11 +72,10 @@ public void testMetrics() throws Exception {
7572
@SuppressWarnings("rawtypes")
7673
ResponseEntity<Map> entity = new TestRestTemplate()
7774
.getForEntity("http://localhost:" + this.port + "/metrics", Map.class);
78-
assertEquals(HttpStatus.OK, entity.getStatusCode());
75+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
7976
@SuppressWarnings("unchecked")
8077
Map<String, Object> body = entity.getBody();
81-
assertTrue("Wrong body: " + body,
82-
body.containsKey("counter.status.401.unmapped"));
78+
assertThat(body).containsKey("counter.status.401.unmapped");
8379
}
8480

8581
}

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
import org.springframework.test.annotation.DirtiesContext;
3131
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3232

33-
import static org.junit.Assert.assertEquals;
34-
import static org.junit.Assert.assertFalse;
33+
import static org.assertj.core.api.Assertions.assertThat;
3534

3635
/**
3736
* Integration tests for insecured service endpoints (even with Spring Security on
@@ -53,12 +52,11 @@ public void testHome() throws Exception {
5352
@SuppressWarnings("rawtypes")
5453
ResponseEntity<Map> entity = new TestRestTemplate()
5554
.getForEntity("http://localhost:" + this.port, Map.class);
56-
assertEquals(HttpStatus.OK, entity.getStatusCode());
55+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
5756
@SuppressWarnings("unchecked")
5857
Map<String, Object> body = entity.getBody();
59-
assertEquals("Hello Phil", body.get("message"));
60-
assertFalse("Wrong headers: " + entity.getHeaders(),
61-
entity.getHeaders().containsKey("Set-Cookie"));
58+
assertThat(body.get("message")).isEqualTo("Hello Phil");
59+
assertThat(entity.getHeaders()).doesNotContainKey("Set-Cookie");
6260
}
6361

6462
}

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
import org.springframework.test.annotation.DirtiesContext;
3131
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3232

33-
import static org.junit.Assert.assertEquals;
34-
import static org.junit.Assert.assertTrue;
33+
import static org.assertj.core.api.Assertions.assertThat;
3534

3635
/**
3736
* Integration tests for separate management and main service ports.
@@ -56,17 +55,16 @@ public void testHome() throws Exception {
5655
@SuppressWarnings("rawtypes")
5756
ResponseEntity<Map> entity = new TestRestTemplate()
5857
.getForEntity("http://localhost:" + this.port, Map.class);
59-
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
58+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
6059
}
6160

6261
@Test
6362
public void testHealth() throws Exception {
6463
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
6564
"http://localhost:" + this.managementPort + "/admin/health",
6665
String.class);
67-
assertEquals(HttpStatus.OK, entity.getStatusCode());
68-
assertTrue("Wrong body: " + entity.getBody(),
69-
entity.getBody().contains("\"status\":\"UP\""));
66+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
67+
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
7068
}
7169

7270
}

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

+6-10
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
import org.springframework.test.annotation.DirtiesContext;
3131
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3232

33-
import static org.junit.Assert.assertEquals;
34-
import static org.junit.Assert.assertFalse;
35-
import static org.junit.Assert.assertTrue;
33+
import static org.assertj.core.api.Assertions.assertThat;
3634

3735
/**
3836
* Integration tests for endpoints configuration.
@@ -52,22 +50,20 @@ public class ManagementPathSampleActuatorApplicationTests {
5250
public void testHealth() throws Exception {
5351
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
5452
"http://localhost:" + this.port + "/admin/health", String.class);
55-
assertEquals(HttpStatus.OK, entity.getStatusCode());
56-
assertTrue("Wrong body: " + entity.getBody(),
57-
entity.getBody().contains("\"status\":\"UP\""));
53+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
54+
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
5855
}
5956

6057
@Test
6158
public void testHomeIsSecure() throws Exception {
6259
@SuppressWarnings("rawtypes")
6360
ResponseEntity<Map> entity = new TestRestTemplate()
6461
.getForEntity("http://localhost:" + this.port, Map.class);
65-
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
62+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
6663
@SuppressWarnings("unchecked")
6764
Map<String, Object> body = entity.getBody();
68-
assertEquals("Wrong body: " + body, "Unauthorized", body.get("error"));
69-
assertFalse("Wrong headers: " + entity.getHeaders(),
70-
entity.getHeaders().containsKey("Set-Cookie"));
65+
assertThat(body.get("error")).isEqualTo("Unauthorized");
66+
assertThat(entity.getHeaders()).doesNotContainKey("Set-Cookie");
7167
}
7268

7369
}

0 commit comments

Comments
 (0)