Skip to content

Commit 9fc3143

Browse files
committed
Polished unit tests.
1 parent 844a9d6 commit 9fc3143

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

src/test/java/de/ksbrwsk/people/PersonHandlerTest.java

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
import reactor.core.publisher.Mono;
1818

1919
import static de.ksbrwsk.people.Constants.API;
20-
import static org.junit.jupiter.api.Assertions.assertEquals;
21-
import static org.junit.jupiter.api.Assertions.assertNotNull;
20+
import static org.assertj.core.api.Assertions.assertThat;
2221
import static org.mockito.ArgumentMatchers.any;
2322
import static org.mockito.Mockito.when;
2423

@@ -46,35 +45,31 @@ void should_handle_find_all() {
4645
.uri(API)
4746
.exchange()
4847
.expectStatus()
49-
.is2xxSuccessful()
50-
.expectBody()
51-
.jsonPath("$[0].id")
52-
.isEqualTo("1")
53-
.jsonPath("$[0].name")
54-
.isEqualTo("Name")
55-
.jsonPath("$[1].id")
56-
.isEqualTo("2")
57-
.jsonPath("$[1].name")
58-
.isEqualTo("Sabo");
48+
.isOk()
49+
.expectBodyList(Person.class)
50+
.hasSize(2)
51+
.contains(
52+
new Person(1L, "Name"),
53+
new Person(2L, "Sabo"));
5954
}
6055

6156
@Test
6257
@DisplayName("should handle request find by id x")
6358
void should_handle_find_by_id() {
6459
when(this.personRepository.findById(1L))
6560
.thenReturn(Mono.just(new Person(1L, "Name")));
66-
Person person = this.webTestClient
61+
this.webTestClient
6762
.get()
6863
.uri(API + "/1")
6964
.exchange()
7065
.expectStatus()
71-
.is2xxSuccessful()
66+
.isOk()
7267
.expectBody(Person.class)
73-
.returnResult()
74-
.getResponseBody();
75-
assertNotNull(person);
76-
assertEquals(person.getName(), "Name");
77-
assertEquals(person.getId(), 1L);
68+
.consumeWith(response -> {
69+
assertThat(response.getResponseBody()).isNotNull();
70+
assertThat(response.getResponseBody().getName()).isEqualTo("Name");
71+
assertThat(response.getResponseBody().getId()).isEqualTo(1L);
72+
});
7873
}
7974

8075
@Test
@@ -173,7 +168,7 @@ void should_handle_update_person(String name) {
173168
.bodyValue(person)
174169
.exchange()
175170
.expectStatus()
176-
.is2xxSuccessful()
171+
.isOk()
177172
.expectBody(Person.class)
178173
.isEqualTo(person);
179174
}
@@ -207,7 +202,7 @@ void should_handle_not_found() {
207202
.uri("/api/peple")
208203
.exchange()
209204
.expectStatus()
210-
.is4xxClientError();
205+
.isNotFound();
211206
}
212207

213208
@Test
@@ -220,7 +215,7 @@ void should_handle_find_first_by_name() {
220215
.uri(API + "/firstByName/First")
221216
.exchange()
222217
.expectStatus()
223-
.is2xxSuccessful()
218+
.isOk()
224219
.expectBody(Person.class)
225220
.isEqualTo(new Person(1L, "First"));
226221
}

src/test/java/de/ksbrwsk/people/PersonTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@
77
import org.junit.jupiter.params.provider.ValueSource;
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.boot.test.context.SpringBootTest;
10-
import org.springframework.test.context.TestPropertySource;
1110

1211
import static org.assertj.core.api.Assertions.assertThat;
1312

1413
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
15-
@TestPropertySource(properties = {"spring.autoconfigure.exclude=" +
16-
"org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration"})
1714
class PersonTest {
1815
@Autowired
1916
Validator validator;

src/test/java/de/ksbrwsk/people/WebIntegrationTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,11 @@ void handleFindAll() {
176176
.expectStatus()
177177
.isOk()
178178
.expectBody()
179-
.jsonPath("$.[0].name").isEqualTo("Person@1")
180-
.jsonPath("$.[1].name").isEqualTo("Person@2");
179+
.jsonPath("$").isArray()
180+
.jsonPath("$[0].id").exists()
181+
.jsonPath("$[0].name").isEqualTo("Person@1")
182+
.jsonPath("$[1].id").exists()
183+
.jsonPath("$[1].name").isEqualTo("Person@2");
181184
}
182185

183186
@Test

0 commit comments

Comments
 (0)