Skip to content

Commit 4a3d229

Browse files
committed
fix and cleanup tests
1 parent 8814157 commit 4a3d229

File tree

3 files changed

+58
-59
lines changed

3 files changed

+58
-59
lines changed

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
@@ -11,7 +11,8 @@
1111
import org.springframework.beans.factory.annotation.Autowired;
1212

1313
import static org.assertj.core.api.Assertions.assertThat;
14-
import static org.junit.jupiter.api.Assertions.assertThrows;
14+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
15+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
1516

1617
@ApplicationTest
1718
public class QueryTransformerTest {
@@ -139,12 +140,10 @@ void givenQueryWithMultipleResults_shouldFail() {
139140

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

142-
assertThrows(
143-
IllegalStateException.class,
144-
() -> QueryTransformer
145-
.of(entityManager, TestModelContainer.class)
146-
.withQuery(query)
147-
.asSingleResult()
143+
assertThatIllegalStateException().isThrownBy(() -> QueryTransformer
144+
.of(entityManager, TestModelContainer.class)
145+
.withQuery(query)
146+
.asSingleResult()
148147
);
149148
}
150149
}
@@ -170,11 +169,10 @@ void givenQueryWithOneResult_shouldPass() {
170169
void givenQueryWithOneResult_shouldFail() {
171170
var query = entityManager.createQuery("select q, 'xxx' from QueryTransformerTestModel q");
172171

173-
assertThrows(
174-
EntityNotFoundException.class, () -> QueryTransformer
175-
.of(entityManager, TestModelContainer.class)
176-
.withQuery(query)
177-
.asSingleResultOrFail()
172+
assertThatExceptionOfType(EntityNotFoundException.class).isThrownBy(() -> QueryTransformer
173+
.of(entityManager, TestModelContainer.class)
174+
.withQuery(query)
175+
.asSingleResultOrFail()
178176
);
179177
}
180178
}
@@ -220,12 +218,10 @@ void givenQuery_wrongTargetClass_shouldFail() {
220218

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

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

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

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

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

1313
import static org.assertj.core.api.Assertions.assertThat;
1414
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
15-
import static org.junit.jupiter.api.Assertions.assertEquals;
16-
import static org.junit.jupiter.api.Assertions.assertTrue;
1715

1816
class CollectUtilTest {
1917
@Nested
@@ -57,7 +55,7 @@ void shouldHandleEmptyCollectionWhenConvertingToSet() {
5755
var result = CollectUtil.collectToSet(emptyList, mapper);
5856

5957
// then
60-
assertTrue(result.isEmpty());
58+
assertThat(result).isEmpty();
6159
}
6260

6361
@Test
@@ -71,8 +69,9 @@ void shouldRemoveDuplicatesWhenConvertingCollectionToSet() {
7169
var result = CollectUtil.collectToSet(numbersWithDuplicates, mapper);
7270

7371
// then
74-
assertThat(result).containsExactlyInAnyOrder("1", "2", "3");
75-
assertEquals(3, result.size());
72+
assertThat(result)
73+
.containsExactlyInAnyOrder("1", "2", "3")
74+
.hasSize(3);
7675
}
7776

7877
@Test
@@ -114,7 +113,7 @@ void shouldHandleEmptyStreamableWhenConvertingToSet() {
114113
var result = CollectUtil.collectToSet(emptyStreamable, mapper);
115114

116115
// then
117-
assertTrue(result.isEmpty());
116+
assertThat(result).isEmpty();
118117
}
119118

120119
@Test
@@ -142,7 +141,7 @@ void shouldHandleEmptyStreamWhenConvertingToSet() {
142141
var result = CollectUtil.collectToSet(emptyStream, mapper);
143142

144143
// then
145-
assertTrue(result.isEmpty());
144+
assertThat(result).isEmpty();
146145
}
147146

148147
@Test
@@ -156,8 +155,9 @@ void shouldRemoveDuplicatesWhenConvertingStreamToSet() {
156155
var result = CollectUtil.collectToSet(numbersWithDuplicates, mapper);
157156

158157
// then
159-
assertThat(result).containsExactlyInAnyOrder("1", "2", "3");
160-
assertEquals(3, result.size());
158+
assertThat(result)
159+
.containsExactlyInAnyOrder("1", "2", "3")
160+
.hasSize(3);
161161
}
162162
}
163163

@@ -188,7 +188,7 @@ void shouldHandleEmptyCollectionWhenConvertingToList() {
188188
var result = CollectUtil.collectToList(emptyList, mapper);
189189

190190
// then
191-
assertTrue(result.isEmpty());
191+
assertThat(result).isEmpty();
192192
}
193193

194194
@Test
@@ -202,8 +202,9 @@ void shouldPreserveDuplicatesWhenConvertingCollectionToList() {
202202
var result = CollectUtil.collectToList(numbersWithDuplicates, mapper);
203203

204204
// then
205-
assertThat(result).containsExactly("1", "2", "2", "3", "3", "3");
206-
assertEquals(6, result.size());
205+
assertThat(result)
206+
.containsExactly("1", "2", "2", "3", "3", "3")
207+
.hasSize(6);
207208
}
208209

209210
@Test
@@ -231,7 +232,7 @@ void shouldHandleEmptyStreamableWhenConvertingToList() {
231232
var result = CollectUtil.collectToList(emptyStreamable, mapper);
232233

233234
// then
234-
assertTrue(result.isEmpty());
235+
assertThat(result).isEmpty();
235236
}
236237

237238
@Test
@@ -259,7 +260,7 @@ void shouldHandleEmptyStreamWhenConvertingToList() {
259260
var result = CollectUtil.collectToList(emptyStream, mapper);
260261

261262
// then
262-
assertTrue(result.isEmpty());
263+
assertThat(result).isEmpty();
263264
}
264265

265266
@Test
@@ -273,8 +274,9 @@ void shouldPreserveDuplicatesWhenConvertingStreamToList() {
273274
var result = CollectUtil.collectToList(numbersWithDuplicates, mapper);
274275

275276
// then
276-
assertThat(result).containsExactly("1", "2", "2", "3", "3", "3");
277-
assertEquals(6, result.size());
277+
assertThat(result)
278+
.containsExactly("1", "2", "2", "3", "3", "3")
279+
.hasSize(6);
278280
}
279281
}
280282

@@ -307,7 +309,7 @@ void shouldHandleEmptyCollectionWhenConvertingToStream() {
307309
var result = resultStream.toList();
308310

309311
// then
310-
assertTrue(result.isEmpty());
312+
assertThat(result).isEmpty();
311313
}
312314

313315
@Test
@@ -337,7 +339,7 @@ void shouldHandleEmptyStreamableWhenConvertingToStream() {
337339
var result = resultStream.toList();
338340

339341
// then
340-
assertTrue(result.isEmpty());
342+
assertThat(result).isEmpty();
341343
}
342344

343345
@Test
@@ -367,7 +369,7 @@ void shouldHandleEmptyStreamWhenConvertingToStream() {
367369
var result = resultStream.toList();
368370

369371
// then
370-
assertTrue(result.isEmpty());
372+
assertThat(result).isEmpty();
371373
}
372374
}
373375

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

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

1212
import static org.assertj.core.api.Assertions.assertThat;
13-
import static org.junit.jupiter.api.Assertions.assertTrue;
1413

1514
class FilterUtilTest {
1615
@Nested
@@ -38,7 +37,7 @@ void shouldHandleEmptyCollectionWhenFilteringToSet() {
3837
var result = FilterUtil.filterToSet(emptyList, n -> n > 2);
3938

4039
// then
41-
assertTrue(result.isEmpty());
40+
assertThat(result).isEmpty();
4241
}
4342

4443
@Test
@@ -51,8 +50,9 @@ void shouldRemoveDuplicatesWhenFilteringCollectionToSet() {
5150
var result = FilterUtil.filterToSet(numbersWithDuplicates, n -> n >= 2);
5251

5352
// then
54-
assertThat(result).containsExactlyInAnyOrder(2, 3);
55-
assertThat(result).hasSize(2);
53+
assertThat(result)
54+
.hasSize(2)
55+
.containsExactlyInAnyOrder(2, 3);
5656
}
5757

5858
@Test
@@ -78,7 +78,7 @@ void shouldHandleEmptyStreamableWhenFilteringToSet() {
7878
var result = FilterUtil.filterToSet(emptyStreamable, n -> n > 2);
7979

8080
// then
81-
assertTrue(result.isEmpty());
81+
assertThat(result).isEmpty();
8282
}
8383

8484
@Test
@@ -91,8 +91,9 @@ void shouldRemoveDuplicatesWhenFilteringStreamToSet() {
9191
var result = FilterUtil.filterToSet(numbersWithDuplicates, n -> n >= 2);
9292

9393
// then
94-
assertThat(result).containsExactlyInAnyOrder(2, 3);
95-
assertThat(result).hasSize(2);
94+
assertThat(result)
95+
.hasSize(2)
96+
.containsExactlyInAnyOrder(2, 3);
9697
}
9798
}
9899

@@ -121,7 +122,7 @@ void shouldHandleEmptyCollectionWhenFilteringToList() {
121122
var result = FilterUtil.filterToList(emptyList, n -> n > 2);
122123

123124
// then
124-
assertTrue(result.isEmpty());
125+
assertThat(result).isEmpty();
125126
}
126127

127128
@Test
@@ -134,8 +135,9 @@ void shouldPreserveDuplicatesWhenFilteringCollectionToList() {
134135
var result = FilterUtil.filterToList(numbersWithDuplicates, n -> n >= 2);
135136

136137
// then
137-
assertThat(result).containsExactly(2, 2, 3, 3, 3);
138-
assertThat(result).hasSize(5);
138+
assertThat(result)
139+
.hasSize(5)
140+
.containsExactly(2, 2, 3, 3, 3);
139141
}
140142

141143
@Test
@@ -161,7 +163,7 @@ void shouldHandleEmptyStreamableWhenFilteringToList() {
161163
var result = FilterUtil.filterToList(emptyStreamable, n -> n > 2);
162164

163165
// then
164-
assertTrue(result.isEmpty());
166+
assertThat(result).isEmpty();
165167
}
166168

167169
@Test
@@ -174,8 +176,9 @@ void shouldPreserveDuplicatesWhenFilteringStreamToList() {
174176
var result = FilterUtil.filterToList(numbersWithDuplicates, n -> n >= 2);
175177

176178
// then
177-
assertThat(result).containsExactly(2, 2, 3, 3, 3);
178-
assertThat(result).hasSize(5);
179+
assertThat(result)
180+
.hasSize(5)
181+
.containsExactly(2, 2, 3, 3, 3);
179182
}
180183
}
181184

@@ -205,7 +208,7 @@ void shouldHandleEmptyCollectionWhenFilteringToStream() {
205208
var resultStream = FilterUtil.filterToStream(emptyList, n -> n > 2);
206209

207210
// then
208-
assertTrue(resultStream.toList().isEmpty());
211+
assertThat(resultStream.toList()).isEmpty();
209212
}
210213

211214
@Test
@@ -232,7 +235,7 @@ void shouldHandleEmptyStreamableWhenFilteringToStream() {
232235
var resultStream = FilterUtil.filterToStream(emptyStreamable, n -> n > 2);
233236

234237
// then
235-
assertTrue(resultStream.toList().isEmpty());
238+
assertThat(resultStream.toList()).isEmpty();
236239
}
237240

238241
@Test
@@ -259,7 +262,7 @@ void shouldHandleEmptyStreamWhenFilteringToStream() {
259262
var resultStream = FilterUtil.filterToStream(emptyStream, n -> n > 2);
260263

261264
// then
262-
assertTrue(resultStream.toList().isEmpty());
265+
assertThat(resultStream.toList()).isEmpty();
263266
}
264267
}
265268
}

0 commit comments

Comments
 (0)