Skip to content

Commit

Permalink
Polished unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ksbrwsk committed Dec 6, 2023
1 parent 844a9d6 commit 9fc3143
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
39 changes: 17 additions & 22 deletions src/test/java/de/ksbrwsk/people/PersonHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -46,35 +45,31 @@ 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
@DisplayName("should handle request find by id x")
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
Expand Down Expand Up @@ -173,7 +168,7 @@ void should_handle_update_person(String name) {
.bodyValue(person)
.exchange()
.expectStatus()
.is2xxSuccessful()
.isOk()
.expectBody(Person.class)
.isEqualTo(person);
}
Expand Down Expand Up @@ -207,7 +202,7 @@ void should_handle_not_found() {
.uri("/api/peple")
.exchange()
.expectStatus()
.is4xxClientError();
.isNotFound();
}

@Test
Expand All @@ -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"));
}
Expand Down
3 changes: 0 additions & 3 deletions src/test/java/de/ksbrwsk/people/PersonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/test/java/de/ksbrwsk/people/WebIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9fc3143

Please sign in to comment.