From 9fc3143eaceef0376311a0e00e6a8d2974010099 Mon Sep 17 00:00:00 2001 From: Kai Saborowski Date: Wed, 6 Dec 2023 17:47:43 +0100 Subject: [PATCH] Polished unit tests. --- .../de/ksbrwsk/people/PersonHandlerTest.java | 39 ++++++++----------- .../java/de/ksbrwsk/people/PersonTest.java | 3 -- .../de/ksbrwsk/people/WebIntegrationTest.java | 7 +++- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/src/test/java/de/ksbrwsk/people/PersonHandlerTest.java b/src/test/java/de/ksbrwsk/people/PersonHandlerTest.java index 52d3b62..bb2c675 100644 --- a/src/test/java/de/ksbrwsk/people/PersonHandlerTest.java +++ b/src/test/java/de/ksbrwsk/people/PersonHandlerTest.java @@ -17,8 +17,7 @@ import reactor.core.publisher.Mono; import static de.ksbrwsk.people.Constants.API; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @@ -46,16 +45,12 @@ void should_handle_find_all() { .uri(API) .exchange() .expectStatus() - .is2xxSuccessful() - .expectBody() - .jsonPath("$[0].id") - .isEqualTo("1") - .jsonPath("$[0].name") - .isEqualTo("Name") - .jsonPath("$[1].id") - .isEqualTo("2") - .jsonPath("$[1].name") - .isEqualTo("Sabo"); + .isOk() + .expectBodyList(Person.class) + .hasSize(2) + .contains( + new Person(1L, "Name"), + new Person(2L, "Sabo")); } @Test @@ -63,18 +58,18 @@ void should_handle_find_all() { void should_handle_find_by_id() { when(this.personRepository.findById(1L)) .thenReturn(Mono.just(new Person(1L, "Name"))); - Person person = this.webTestClient + this.webTestClient .get() .uri(API + "/1") .exchange() .expectStatus() - .is2xxSuccessful() + .isOk() .expectBody(Person.class) - .returnResult() - .getResponseBody(); - assertNotNull(person); - assertEquals(person.getName(), "Name"); - assertEquals(person.getId(), 1L); + .consumeWith(response -> { + assertThat(response.getResponseBody()).isNotNull(); + assertThat(response.getResponseBody().getName()).isEqualTo("Name"); + assertThat(response.getResponseBody().getId()).isEqualTo(1L); + }); } @Test @@ -173,7 +168,7 @@ void should_handle_update_person(String name) { .bodyValue(person) .exchange() .expectStatus() - .is2xxSuccessful() + .isOk() .expectBody(Person.class) .isEqualTo(person); } @@ -207,7 +202,7 @@ void should_handle_not_found() { .uri("/api/peple") .exchange() .expectStatus() - .is4xxClientError(); + .isNotFound(); } @Test @@ -220,7 +215,7 @@ void should_handle_find_first_by_name() { .uri(API + "/firstByName/First") .exchange() .expectStatus() - .is2xxSuccessful() + .isOk() .expectBody(Person.class) .isEqualTo(new Person(1L, "First")); } diff --git a/src/test/java/de/ksbrwsk/people/PersonTest.java b/src/test/java/de/ksbrwsk/people/PersonTest.java index 7c23fbf..c16e7e3 100644 --- a/src/test/java/de/ksbrwsk/people/PersonTest.java +++ b/src/test/java/de/ksbrwsk/people/PersonTest.java @@ -7,13 +7,10 @@ import org.junit.jupiter.params.provider.ValueSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.TestPropertySource; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) -@TestPropertySource(properties = {"spring.autoconfigure.exclude=" + - "org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration"}) class PersonTest { @Autowired Validator validator; diff --git a/src/test/java/de/ksbrwsk/people/WebIntegrationTest.java b/src/test/java/de/ksbrwsk/people/WebIntegrationTest.java index c826edd..b2ec26d 100644 --- a/src/test/java/de/ksbrwsk/people/WebIntegrationTest.java +++ b/src/test/java/de/ksbrwsk/people/WebIntegrationTest.java @@ -176,8 +176,11 @@ void handleFindAll() { .expectStatus() .isOk() .expectBody() - .jsonPath("$.[0].name").isEqualTo("Person@1") - .jsonPath("$.[1].name").isEqualTo("Person@2"); + .jsonPath("$").isArray() + .jsonPath("$[0].id").exists() + .jsonPath("$[0].name").isEqualTo("Person@1") + .jsonPath("$[1].id").exists() + .jsonPath("$[1].name").isEqualTo("Person@2"); } @Test