Skip to content

Commit 78f3a01

Browse files
committed
fix and cleanup tests
1 parent d39eba5 commit 78f3a01

3 files changed

Lines changed: 58 additions & 59 deletions

File tree

src/test/java/it/aboutbits/springboot/toolbox/persistence/transformer/QueryTransformerTest.java

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
import org.springframework.beans.factory.annotation.Autowired;
1414

1515
import static org.assertj.core.api.Assertions.assertThat;
16-
import static org.junit.jupiter.api.Assertions.assertThrows;
16+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
17+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
1718

1819
@ApplicationTest
1920
@NullMarked
@@ -141,12 +142,10 @@ void givenQueryWithMultipleResults_shouldFail() {
141142

142143
var query = entityManager.createQuery("select q, 'xxx' from QueryTransformerTestModel q");
143144

144-
assertThrows(
145-
IllegalStateException.class,
146-
() -> QueryTransformer
147-
.of(entityManager, TestModelContainer.class)
148-
.withQuery(query)
149-
.asSingleResult()
145+
assertThatIllegalStateException().isThrownBy(() -> QueryTransformer
146+
.of(entityManager, TestModelContainer.class)
147+
.withQuery(query)
148+
.asSingleResult()
150149
);
151150
}
152151
}
@@ -172,11 +171,10 @@ void givenQueryWithOneResult_shouldPass() {
172171
void givenQueryWithOneResult_shouldFail() {
173172
var query = entityManager.createQuery("select q, 'xxx' from QueryTransformerTestModel q");
174173

175-
assertThrows(
176-
EntityNotFoundException.class, () -> QueryTransformer
177-
.of(entityManager, TestModelContainer.class)
178-
.withQuery(query)
179-
.asSingleResultOrFail()
174+
assertThatExceptionOfType(EntityNotFoundException.class).isThrownBy(() -> QueryTransformer
175+
.of(entityManager, TestModelContainer.class)
176+
.withQuery(query)
177+
.asSingleResultOrFail()
180178
);
181179
}
182180
}
@@ -221,12 +219,10 @@ void givenQuery_wrongTargetClass_shouldFail() {
221219

222220
var query = entityManager.createQuery("select q, 'xxx' from QueryTransformerTestModel q");
223221

224-
assertThrows(
225-
TransformerRuntimeException.class,
226-
() -> QueryTransformer
227-
.of(entityManager, WrongContainer.class)
228-
.withQuery(query)
229-
.asList()
222+
assertThatExceptionOfType(TransformerRuntimeException.class).isThrownBy(() -> QueryTransformer
223+
.of(entityManager, WrongContainer.class)
224+
.withQuery(query)
225+
.asList()
230226
);
231227
}
232228
}
@@ -351,12 +347,10 @@ void givenVariousQueries_shouldPassReturningTheRightTotalCount() {
351347
void givenQueryWithSelectDistinct_shouldFail() {
352348
var query = entityManager.createQuery("select distinct q, 'xxx' from QueryTransformerTestModel q");
353349

354-
assertThrows(
355-
IllegalStateException.class,
356-
() -> QueryTransformer
357-
.of(entityManager, TestModelContainer.class)
358-
.withQuery(query)
359-
.asPage(1, 2)
350+
assertThatIllegalStateException().isThrownBy(() -> QueryTransformer
351+
.of(entityManager, TestModelContainer.class)
352+
.withQuery(query)
353+
.asPage(1, 2)
360354
);
361355
}
362356
}

src/test/java/it/aboutbits/springboot/toolbox/util/CollectUtilTest.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
import static org.assertj.core.api.Assertions.assertThat;
1616
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
17-
import static org.junit.jupiter.api.Assertions.assertEquals;
18-
import static org.junit.jupiter.api.Assertions.assertTrue;
1917

2018
@NullMarked
2119
class CollectUtilTest {
@@ -60,7 +58,7 @@ void shouldHandleEmptyCollectionWhenConvertingToSet() {
6058
var result = CollectUtil.collectToSet(emptyList, mapper);
6159

6260
// then
63-
assertTrue(result.isEmpty());
61+
assertThat(result).isEmpty();
6462
}
6563

6664
@Test
@@ -74,8 +72,9 @@ void shouldRemoveDuplicatesWhenConvertingCollectionToSet() {
7472
var result = CollectUtil.collectToSet(numbersWithDuplicates, mapper);
7573

7674
// then
77-
assertThat(result).containsExactlyInAnyOrder("1", "2", "3");
78-
assertEquals(3, result.size());
75+
assertThat(result)
76+
.containsExactlyInAnyOrder("1", "2", "3")
77+
.hasSize(3);
7978
}
8079

8180
@Test
@@ -117,7 +116,7 @@ void shouldHandleEmptyStreamableWhenConvertingToSet() {
117116
var result = CollectUtil.collectToSet(emptyStreamable, mapper);
118117

119118
// then
120-
assertTrue(result.isEmpty());
119+
assertThat(result).isEmpty();
121120
}
122121

123122
@Test
@@ -145,7 +144,7 @@ void shouldHandleEmptyStreamWhenConvertingToSet() {
145144
var result = CollectUtil.collectToSet(emptyStream, mapper);
146145

147146
// then
148-
assertTrue(result.isEmpty());
147+
assertThat(result).isEmpty();
149148
}
150149

151150
@Test
@@ -159,8 +158,9 @@ void shouldRemoveDuplicatesWhenConvertingStreamToSet() {
159158
var result = CollectUtil.collectToSet(numbersWithDuplicates, mapper);
160159

161160
// then
162-
assertThat(result).containsExactlyInAnyOrder("1", "2", "3");
163-
assertEquals(3, result.size());
161+
assertThat(result)
162+
.containsExactlyInAnyOrder("1", "2", "3")
163+
.hasSize(3);
164164
}
165165
}
166166

@@ -191,7 +191,7 @@ void shouldHandleEmptyCollectionWhenConvertingToList() {
191191
var result = CollectUtil.collectToList(emptyList, mapper);
192192

193193
// then
194-
assertTrue(result.isEmpty());
194+
assertThat(result).isEmpty();
195195
}
196196

197197
@Test
@@ -205,8 +205,9 @@ void shouldPreserveDuplicatesWhenConvertingCollectionToList() {
205205
var result = CollectUtil.collectToList(numbersWithDuplicates, mapper);
206206

207207
// then
208-
assertThat(result).containsExactly("1", "2", "2", "3", "3", "3");
209-
assertEquals(6, result.size());
208+
assertThat(result)
209+
.containsExactly("1", "2", "2", "3", "3", "3")
210+
.hasSize(6);
210211
}
211212

212213
@Test
@@ -234,7 +235,7 @@ void shouldHandleEmptyStreamableWhenConvertingToList() {
234235
var result = CollectUtil.collectToList(emptyStreamable, mapper);
235236

236237
// then
237-
assertTrue(result.isEmpty());
238+
assertThat(result).isEmpty();
238239
}
239240

240241
@Test
@@ -262,7 +263,7 @@ void shouldHandleEmptyStreamWhenConvertingToList() {
262263
var result = CollectUtil.collectToList(emptyStream, mapper);
263264

264265
// then
265-
assertTrue(result.isEmpty());
266+
assertThat(result).isEmpty();
266267
}
267268

268269
@Test
@@ -276,8 +277,9 @@ void shouldPreserveDuplicatesWhenConvertingStreamToList() {
276277
var result = CollectUtil.collectToList(numbersWithDuplicates, mapper);
277278

278279
// then
279-
assertThat(result).containsExactly("1", "2", "2", "3", "3", "3");
280-
assertEquals(6, result.size());
280+
assertThat(result)
281+
.containsExactly("1", "2", "2", "3", "3", "3")
282+
.hasSize(6);
281283
}
282284
}
283285

@@ -310,7 +312,7 @@ void shouldHandleEmptyCollectionWhenConvertingToStream() {
310312
var result = resultStream.toList();
311313

312314
// then
313-
assertTrue(result.isEmpty());
315+
assertThat(result).isEmpty();
314316
}
315317

316318
@Test
@@ -340,7 +342,7 @@ void shouldHandleEmptyStreamableWhenConvertingToStream() {
340342
var result = resultStream.toList();
341343

342344
// then
343-
assertTrue(result.isEmpty());
345+
assertThat(result).isEmpty();
344346
}
345347

346348
@Test
@@ -370,7 +372,7 @@ void shouldHandleEmptyStreamWhenConvertingToStream() {
370372
var result = resultStream.toList();
371373

372374
// then
373-
assertTrue(result.isEmpty());
375+
assertThat(result).isEmpty();
374376
}
375377
}
376378

src/test/java/it/aboutbits/springboot/toolbox/util/FilterUtilTest.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.util.stream.Stream;
1212

1313
import static org.assertj.core.api.Assertions.assertThat;
14-
import static org.junit.jupiter.api.Assertions.assertTrue;
1514

1615
@NullMarked
1716
class FilterUtilTest {
@@ -40,7 +39,7 @@ void shouldHandleEmptyCollectionWhenFilteringToSet() {
4039
var result = FilterUtil.filterToSet(emptyList, n -> n > 2);
4140

4241
// then
43-
assertTrue(result.isEmpty());
42+
assertThat(result).isEmpty();
4443
}
4544

4645
@Test
@@ -53,8 +52,9 @@ void shouldRemoveDuplicatesWhenFilteringCollectionToSet() {
5352
var result = FilterUtil.filterToSet(numbersWithDuplicates, n -> n >= 2);
5453

5554
// then
56-
assertThat(result).containsExactlyInAnyOrder(2, 3);
57-
assertThat(result).hasSize(2);
55+
assertThat(result)
56+
.hasSize(2)
57+
.containsExactlyInAnyOrder(2, 3);
5858
}
5959

6060
@Test
@@ -80,7 +80,7 @@ void shouldHandleEmptyStreamableWhenFilteringToSet() {
8080
var result = FilterUtil.filterToSet(emptyStreamable, n -> n > 2);
8181

8282
// then
83-
assertTrue(result.isEmpty());
83+
assertThat(result).isEmpty();
8484
}
8585

8686
@Test
@@ -93,8 +93,9 @@ void shouldRemoveDuplicatesWhenFilteringStreamToSet() {
9393
var result = FilterUtil.filterToSet(numbersWithDuplicates, n -> n >= 2);
9494

9595
// then
96-
assertThat(result).containsExactlyInAnyOrder(2, 3);
97-
assertThat(result).hasSize(2);
96+
assertThat(result)
97+
.hasSize(2)
98+
.containsExactlyInAnyOrder(2, 3);
9899
}
99100
}
100101

@@ -123,7 +124,7 @@ void shouldHandleEmptyCollectionWhenFilteringToList() {
123124
var result = FilterUtil.filterToList(emptyList, n -> n > 2);
124125

125126
// then
126-
assertTrue(result.isEmpty());
127+
assertThat(result).isEmpty();
127128
}
128129

129130
@Test
@@ -136,8 +137,9 @@ void shouldPreserveDuplicatesWhenFilteringCollectionToList() {
136137
var result = FilterUtil.filterToList(numbersWithDuplicates, n -> n >= 2);
137138

138139
// then
139-
assertThat(result).containsExactly(2, 2, 3, 3, 3);
140-
assertThat(result).hasSize(5);
140+
assertThat(result)
141+
.hasSize(5)
142+
.containsExactly(2, 2, 3, 3, 3);
141143
}
142144

143145
@Test
@@ -163,7 +165,7 @@ void shouldHandleEmptyStreamableWhenFilteringToList() {
163165
var result = FilterUtil.filterToList(emptyStreamable, n -> n > 2);
164166

165167
// then
166-
assertTrue(result.isEmpty());
168+
assertThat(result).isEmpty();
167169
}
168170

169171
@Test
@@ -176,8 +178,9 @@ void shouldPreserveDuplicatesWhenFilteringStreamToList() {
176178
var result = FilterUtil.filterToList(numbersWithDuplicates, n -> n >= 2);
177179

178180
// then
179-
assertThat(result).containsExactly(2, 2, 3, 3, 3);
180-
assertThat(result).hasSize(5);
181+
assertThat(result)
182+
.hasSize(5)
183+
.containsExactly(2, 2, 3, 3, 3);
181184
}
182185
}
183186

@@ -207,7 +210,7 @@ void shouldHandleEmptyCollectionWhenFilteringToStream() {
207210
var resultStream = FilterUtil.filterToStream(emptyList, n -> n > 2);
208211

209212
// then
210-
assertTrue(resultStream.toList().isEmpty());
213+
assertThat(resultStream.toList()).isEmpty();
211214
}
212215

213216
@Test
@@ -234,7 +237,7 @@ void shouldHandleEmptyStreamableWhenFilteringToStream() {
234237
var resultStream = FilterUtil.filterToStream(emptyStreamable, n -> n > 2);
235238

236239
// then
237-
assertTrue(resultStream.toList().isEmpty());
240+
assertThat(resultStream.toList()).isEmpty();
238241
}
239242

240243
@Test
@@ -261,7 +264,7 @@ void shouldHandleEmptyStreamWhenFilteringToStream() {
261264
var resultStream = FilterUtil.filterToStream(emptyStream, n -> n > 2);
262265

263266
// then
264-
assertTrue(resultStream.toList().isEmpty());
267+
assertThat(resultStream.toList()).isEmpty();
265268
}
266269
}
267270
}

0 commit comments

Comments
 (0)