Simplify AssertJ assertions and also make them more readable

See gh-33653
This commit is contained in:
Krzysztof Krason
2022-12-29 17:52:45 +01:00
committed by Moritz Halbritter
parent c9a2b2ab66
commit cf6493f65c
345 changed files with 1258 additions and 1292 deletions

View File

@@ -51,7 +51,7 @@ abstract class AbstractManagementPortAndPathSampleActuatorApplicationTests {
ResponseEntity<Map<String, Object>> entity = asMapEntity(
new TestRestTemplate("user", "password").getForEntity("http://localhost:" + this.port, Map.class));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody().get("message")).isEqualTo("Hello Phil");
assertThat(entity.getBody()).containsEntry("message", "Hello Phil");
}
@Test
@@ -101,7 +101,7 @@ abstract class AbstractManagementPortAndPathSampleActuatorApplicationTests {
ResponseEntity<Map<String, Object>> entity = asMapEntity(new TestRestTemplate("user", "password")
.getForEntity("http://localhost:" + this.port + "/error", Map.class));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
assertThat(entity.getBody().get("status")).isEqualTo(999);
assertThat(entity.getBody()).containsEntry("status", 999);
}
@Test
@@ -109,7 +109,7 @@ abstract class AbstractManagementPortAndPathSampleActuatorApplicationTests {
ResponseEntity<Map<String, Object>> entity = asMapEntity(new TestRestTemplate("user", "password")
.getForEntity("http://localhost:" + this.managementPort + "/error", Map.class));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody().get("status")).isEqualTo(999);
assertThat(entity.getBody()).containsEntry("status", 999);
}
@SuppressWarnings({ "unchecked", "rawtypes" })

View File

@@ -48,8 +48,8 @@ class EndpointsPropertiesSampleActuatorApplicationTests {
this.restTemplate.withBasicAuth("user", "password").getForEntity("/oops", Map.class));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
Map<String, Object> body = entity.getBody();
assertThat(body.get("error")).isEqualTo("None");
assertThat(body.get("status")).isEqualTo(999);
assertThat(body).containsEntry("error", "None");
assertThat(body).containsEntry("status", 999);
}
@Test

View File

@@ -62,7 +62,7 @@ class ManagementPortSampleActuatorApplicationTests {
ResponseEntity<Map<String, Object>> entity = asMapEntity(
new TestRestTemplate("user", "password").getForEntity("http://localhost:" + this.port, Map.class));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody().get("message")).isEqualTo("Hello Phil");
assertThat(entity.getBody()).containsEntry("message", "Hello Phil");
}
@Test
@@ -88,7 +88,7 @@ class ManagementPortSampleActuatorApplicationTests {
ResponseEntity<Map<String, Object>> entity = asMapEntity(new TestRestTemplate("user", "password")
.getForEntity("http://localhost:" + this.managementPort + "/error", Map.class));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody().get("status")).isEqualTo(999);
assertThat(entity.getBody()).containsEntry("status", 999);
}
@Test
@@ -98,7 +98,7 @@ class ManagementPortSampleActuatorApplicationTests {
.getForEntity("http://localhost:" + this.managementPort + "/404", Map.class));
assertThat(this.errorAttributes.securityContext.getAuthentication()).isNotNull();
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(entity.getBody().get("status")).isEqualTo(404);
assertThat(entity.getBody()).containsEntry("status", 404);
}
@SuppressWarnings({ "unchecked", "rawtypes" })

View File

@@ -45,7 +45,7 @@ class NoManagementSampleActuatorApplicationTests {
ResponseEntity<Map<String, Object>> entity = asMapEntity(
this.restTemplate.withBasicAuth("user", "password").getForEntity("/", Map.class));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody().get("message")).isEqualTo("Hello Phil");
assertThat(entity.getBody()).containsEntry("message", "Hello Phil");
}
@Test

View File

@@ -78,7 +78,7 @@ class SampleActuatorApplicationTests {
ResponseEntity<Map<String, Object>> entity = asMapEntity(
this.restTemplate.withBasicAuth("user", "password").getForEntity("/", Map.class));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody().get("message")).isEqualTo("Hello Phil");
assertThat(entity.getBody()).containsEntry("message", "Hello Phil");
}
@Test
@@ -136,8 +136,8 @@ class SampleActuatorApplicationTests {
ResponseEntity<Map<String, Object>> entity = asMapEntity(
this.restTemplate.withBasicAuth("user", "password").getForEntity("/error", Map.class));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
assertThat(entity.getBody().get("error")).isEqualTo("None");
assertThat(entity.getBody().get("status")).isEqualTo(999);
assertThat(entity.getBody()).containsEntry("error", "None");
assertThat(entity.getBody()).containsEntry("status", 999);
}
@Test

View File

@@ -45,8 +45,8 @@ class ServletPathSampleActuatorApplicationTests {
ResponseEntity<Map<String, Object>> entity = asMapEntity(
this.restTemplate.withBasicAuth("user", "password").getForEntity("/spring/error", Map.class));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
assertThat(entity.getBody().get("error")).isEqualTo("None");
assertThat(entity.getBody().get("status")).isEqualTo(999);
assertThat(entity.getBody()).containsEntry("error", "None");
assertThat(entity.getBody()).containsEntry("status", 999);
}
@Test

View File

@@ -52,7 +52,7 @@ class ShutdownSampleActuatorApplicationTests {
this.restTemplate.withBasicAuth("user", "password").getForEntity("/", Map.class));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
Map<String, Object> body = entity.getBody();
assertThat(body.get("message")).isEqualTo("Hello Phil");
assertThat(body).containsEntry("message", "Hello Phil");
}
@Test

View File

@@ -41,7 +41,7 @@ public class SampleAntApplicationIT {
Process process = new JavaExecutable().processBuilder("-jar", "spring-boot-smoke-test-ant.jar").directory(libs)
.start();
process.waitFor(5, TimeUnit.MINUTES);
assertThat(process.exitValue()).isEqualTo(0);
assertThat(process.exitValue()).isZero();
String output = FileCopyUtils.copyToString(new InputStreamReader(process.getInputStream()));
assertThat(output).contains("Spring Boot Ant Example");
}

View File

@@ -30,7 +30,7 @@ class SampleBatchApplicationTests {
@Test
void testDefaultSettings(CapturedOutput output) {
assertThat(SpringApplication.exit(SpringApplication.run(SampleBatchApplication.class))).isEqualTo(0);
assertThat(SpringApplication.exit(SpringApplication.run(SampleBatchApplication.class))).isZero();
assertThat(output).contains("completed with the following parameters");
}

View File

@@ -44,7 +44,7 @@ class JpaNoteRepositoryIntegrationTests {
List<Note> notes = this.repository.findAll();
assertThat(notes).hasSize(4);
for (Note note : notes) {
assertThat(note.getTags().size()).isGreaterThan(0);
assertThat(note.getTags()).isNotEmpty();
}
}

View File

@@ -44,7 +44,7 @@ class JpaTagRepositoryIntegrationTests {
List<Tag> tags = this.repository.findAll();
assertThat(tags).hasSize(3);
for (Tag tag : tags) {
assertThat(tag.getNotes().size()).isGreaterThan(0);
assertThat(tag.getNotes()).isNotEmpty();
}
}

View File

@@ -29,6 +29,7 @@ import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.core.NestedCheckedException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;
@ExtendWith(OutputCaptureExtension.class)
class SampleLiquibaseApplicationTests {
@@ -52,9 +53,7 @@ class SampleLiquibaseApplicationTests {
SampleLiquibaseApplication.main(new String[] { "--server.port=0" });
}
catch (IllegalStateException ex) {
if (serverNotRunning(ex)) {
return;
}
assumeThat(serverNotRunning(ex)).isFalse();
}
assertThat(output).contains("Successfully acquired change log lock")
.contains("Creating database history table with name: PUBLIC.DATABASECHANGELOG")

View File

@@ -65,13 +65,13 @@ class SampleSecureApplicationTests {
void authenticated() {
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("user", "N/A",
AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER")));
assertThat("Hello Security").isEqualTo(this.service.secure());
assertThat(this.service.secure()).isEqualTo("Hello Security");
}
@Test
void preauth() {
SecurityContextHolder.getContext().setAuthentication(this.authentication);
assertThat("Hello World").isEqualTo(this.service.authorized());
assertThat(this.service.authorized()).isEqualTo("Hello World");
}
@Test

View File

@@ -60,7 +60,7 @@ class SampleSessionHazelcastApplicationTests {
assertThat(entity).isNotNull();
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
List<Map<String, Object>> sessions = (List<Map<String, Object>>) entity.getBody().get("sessions");
assertThat(sessions.size()).isEqualTo(1);
assertThat(sessions).hasSize(1);
}
private String performLogin() {

View File

@@ -87,7 +87,7 @@ class SampleSessionJdbcApplicationTests {
assertThat(response).isNotNull();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
List<Map<String, Object>> sessions = (List<Map<String, Object>>) response.getBody().get("sessions");
assertThat(sessions.size()).isEqualTo(1);
assertThat(sessions).hasSize(1);
}
private ResponseEntity<String> performRequest(URI uri, String cookie) {

View File

@@ -79,7 +79,7 @@ class SampleSessionMongoApplicationTests {
assertThat(response).isNotNull();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
List<Map<String, Object>> sessions = (List<Map<String, Object>>) response.getBody().get("sessions");
assertThat(sessions.size()).isEqualTo(1);
assertThat(sessions).hasSize(1);
}
@Test

View File

@@ -73,7 +73,7 @@ class SampleSessionRedisApplicationTests {
assertThat(response).isNotNull();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
List<Map<String, Object>> sessions = (List<Map<String, Object>>) response.getBody().get("sessions");
assertThat(sessions.size()).isEqualTo(1);
assertThat(sessions).hasSize(1);
}
private String performLogin() {

View File

@@ -54,7 +54,7 @@ class VehicleIdentificationNumberTests {
@Test
void toStringShouldReturnVin() {
VehicleIdentificationNumber vin = new VehicleIdentificationNumber(SAMPLE_VIN);
assertThat(vin.toString()).isEqualTo(SAMPLE_VIN);
assertThat(vin).hasToString(SAMPLE_VIN);
}
@Test
@@ -62,7 +62,7 @@ class VehicleIdentificationNumberTests {
VehicleIdentificationNumber vin1 = new VehicleIdentificationNumber(SAMPLE_VIN);
VehicleIdentificationNumber vin2 = new VehicleIdentificationNumber(SAMPLE_VIN);
VehicleIdentificationNumber vin3 = new VehicleIdentificationNumber("00000000000000000");
assertThat(vin1.hashCode()).isEqualTo(vin2.hashCode());
assertThat(vin1).hasSameHashCodeAs(vin2);
assertThat(vin1).isEqualTo(vin1).isEqualTo(vin2).isNotEqualTo(vin3);
}

View File

@@ -59,7 +59,7 @@ class SampleWebSocketsApplicationTests {
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context.getBean(ClientConfiguration.class).messagePayload;
context.close();
assertThat(count).isEqualTo(0);
assertThat(count).isZero();
assertThat(messagePayloadReference.get()).isEqualTo("Did you say \"Hello world!\"?");
}
@@ -72,7 +72,7 @@ class SampleWebSocketsApplicationTests {
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context.getBean(ClientConfiguration.class).messagePayload;
context.close();
assertThat(count).isEqualTo(0);
assertThat(count).isZero();
assertThat(messagePayloadReference.get()).isEqualTo("Reversed: !dlrow olleH");
}

View File

@@ -64,7 +64,7 @@ class CustomContainerWebSocketsApplicationTests {
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context.getBean(ClientConfiguration.class).messagePayload;
context.close();
assertThat(count).isEqualTo(0);
assertThat(count).isZero();
assertThat(messagePayloadReference.get()).isEqualTo("Did you say \"Hello world!\"?");
}
@@ -77,7 +77,7 @@ class CustomContainerWebSocketsApplicationTests {
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context.getBean(ClientConfiguration.class).messagePayload;
context.close();
assertThat(count).isEqualTo(0);
assertThat(count).isZero();
assertThat(messagePayloadReference.get()).isEqualTo("Reversed: !dlrow olleH");
}

View File

@@ -34,7 +34,7 @@ class SnakeTimerTests {
SnakeTimer.addSnake(snake);
SnakeTimer.broadcast("");
assertThat(SnakeTimer.getSnakes()).hasSize(0);
assertThat(SnakeTimer.getSnakes()).isEmpty();
}
}

View File

@@ -59,7 +59,7 @@ class SampleWebSocketsApplicationTests {
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context.getBean(ClientConfiguration.class).messagePayload;
context.close();
assertThat(count).isEqualTo(0);
assertThat(count).isZero();
assertThat(messagePayloadReference.get()).isEqualTo("Did you say \"Hello world!\"?");
}
@@ -72,7 +72,7 @@ class SampleWebSocketsApplicationTests {
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context.getBean(ClientConfiguration.class).messagePayload;
context.close();
assertThat(count).isEqualTo(0);
assertThat(count).isZero();
assertThat(messagePayloadReference.get()).isEqualTo("Reversed: !dlrow olleH");
}

View File

@@ -64,7 +64,7 @@ class CustomContainerWebSocketsApplicationTests {
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context.getBean(ClientConfiguration.class).messagePayload;
context.close();
assertThat(count).isEqualTo(0);
assertThat(count).isZero();
assertThat(messagePayloadReference.get()).isEqualTo("Did you say \"Hello world!\"?");
}
@@ -77,7 +77,7 @@ class CustomContainerWebSocketsApplicationTests {
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context.getBean(ClientConfiguration.class).messagePayload;
context.close();
assertThat(count).isEqualTo(0);
assertThat(count).isZero();
assertThat(messagePayloadReference.get()).isEqualTo("Reversed: !dlrow olleH");
}

View File

@@ -33,7 +33,7 @@ class SnakeTimerTests {
willThrow(new IOException()).given(snake).sendMessage(anyString());
SnakeTimer.addSnake(snake);
SnakeTimer.broadcast("");
assertThat(SnakeTimer.getSnakes()).hasSize(0);
assertThat(SnakeTimer.getSnakes()).isEmpty();
}
}

View File

@@ -59,7 +59,7 @@ class SampleWebSocketsApplicationTests {
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context.getBean(ClientConfiguration.class).messagePayload;
context.close();
assertThat(count).isEqualTo(0);
assertThat(count).isZero();
assertThat(messagePayloadReference.get()).isEqualTo("Did you say \"Hello world!\"?");
}
@@ -72,7 +72,7 @@ class SampleWebSocketsApplicationTests {
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context.getBean(ClientConfiguration.class).messagePayload;
context.close();
assertThat(count).isEqualTo(0);
assertThat(count).isZero();
assertThat(messagePayloadReference.get()).isEqualTo("Reversed: !dlrow olleH");
}

View File

@@ -64,7 +64,7 @@ class CustomContainerWebSocketsApplicationTests {
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context.getBean(ClientConfiguration.class).messagePayload;
context.close();
assertThat(count).isEqualTo(0);
assertThat(count).isZero();
assertThat(messagePayloadReference.get()).isEqualTo("Did you say \"Hello world!\"?");
}
@@ -77,7 +77,7 @@ class CustomContainerWebSocketsApplicationTests {
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context.getBean(ClientConfiguration.class).messagePayload;
context.close();
assertThat(count).isEqualTo(0);
assertThat(count).isZero();
assertThat(messagePayloadReference.get()).isEqualTo("Reversed: !dlrow olleH");
}

View File

@@ -33,7 +33,7 @@ class SnakeTimerTests {
willThrow(new IOException()).given(snake).sendMessage(anyString());
SnakeTimer.addSnake(snake);
SnakeTimer.broadcast("");
assertThat(SnakeTimer.getSnakes()).hasSize(0);
assertThat(SnakeTimer.getSnakes()).isEmpty();
}
}