/
SoftAssertionsTest.java
535 lines (433 loc) · 20 KB
/
SoftAssertionsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2015 the original author or authors.
*/
package org.assertj.core.api;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.shouldHaveThrown;
import static org.assertj.core.api.Assertions.tuple;
import static org.assertj.core.util.DateUtil.parseDatetime;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.assertj.core.api.iterable.Extractor;
import org.assertj.core.data.MapEntry;
import org.assertj.core.test.CartoonCharacter;
import org.assertj.core.test.Maps;
import org.assertj.core.test.Name;
import org.assertj.core.util.Lists;
import org.junit.Before;
import org.junit.Test;
/**
* Tests for <code>{@link SoftAssertions}</code>.
*
* @author Brian Laframboise
*/
public class SoftAssertionsTest {
private SoftAssertions softly;
private CartoonCharacter homer;
private CartoonCharacter fred;
@Before
public void setup() {
softly = new SoftAssertions();
CartoonCharacter bart = new CartoonCharacter("Bart Simpson");
CartoonCharacter lisa = new CartoonCharacter("Lisa Simpson");
CartoonCharacter maggie = new CartoonCharacter("Maggie Simpson");
homer = new CartoonCharacter("Homer Simpson");
homer.getChildren().add(bart);
homer.getChildren().add(lisa);
homer.getChildren().add(maggie);
CartoonCharacter pebbles = new CartoonCharacter("Pebbles Flintstone");
fred = new CartoonCharacter("Fred Flintstone");
fred.getChildren().add(pebbles);
}
@Test
public void all_assertions_should_pass() {
softly.assertThat(1).isEqualTo(1);
softly.assertThat(Lists.newArrayList(1, 2)).containsOnly(1, 2);
softly.assertAll();
}
@SuppressWarnings("unchecked")
@Test
public void should_be_able_to_catch_exceptions_thrown_by_map_assertions() {
try {
softly.assertThat(Maps.mapOf(MapEntry.entry("54", "55"))).contains(MapEntry.entry("1", "2"));
softly.assertAll();
fail("Should not reach here");
} catch (SoftAssertionError e) {
List<String> errors = e.getErrors();
assertThat(errors).contains(String.format("%nExpecting:%n"
+ " <{\"54\"=\"55\"}>%n"
+ "to contain:%n"
+ " <[MapEntry[key=\"1\", value=\"2\"]]>%n"
+ "but could not find:%n"
+ " <[MapEntry[key=\"1\", value=\"2\"]]>%n"));
}
}
@SuppressWarnings("unchecked")
@Test
public void should_be_able_to_catch_exceptions_thrown_by_all_proxied_methods() throws URISyntaxException {
try {
softly.assertThat(BigDecimal.ZERO).isEqualTo(BigDecimal.ONE);
softly.assertThat(Boolean.FALSE).isTrue();
softly.assertThat(false).isTrue();
softly.assertThat(new boolean[] { false }).isEqualTo(new boolean[] { true });
softly.assertThat(new Byte((byte) 0)).isEqualTo((byte) 1);
softly.assertThat((byte) 2).inHexadecimal().isEqualTo((byte) 3);
softly.assertThat(new byte[] { 4 }).isEqualTo(new byte[] { 5 });
softly.assertThat(new Character((char) 65)).isEqualTo(new Character((char) 66));
softly.assertThat((char) 67).isEqualTo((char) 68);
softly.assertThat(new char[] { 69 }).isEqualTo(new char[] { 70 });
softly.assertThat(new StringBuilder("a")).isEqualTo(new StringBuilder("b"));
softly.assertThat(Object.class).isEqualTo(String.class);
softly.assertThat(parseDatetime("1999-12-31T23:59:59")).isEqualTo(parseDatetime("2000-01-01T00:00:01"));
softly.assertThat(new Double(6.0d)).isEqualTo(new Double(7.0d));
softly.assertThat(8.0d).isEqualTo(9.0d);
softly.assertThat(new double[] { 10.0d }).isEqualTo(new double[] { 11.0d });
softly.assertThat(new File("a"))
.overridingErrorMessage("expected:<File(b)> but was:<File(a)>")
.isEqualTo(new File("b"));
softly.assertThat(new Float(12f)).isEqualTo(new Float(13f));
softly.assertThat(14f).isEqualTo(15f);
softly.assertThat(new float[] { 16f }).isEqualTo(new float[] { 17f });
softly.assertThat(new ByteArrayInputStream(new byte[] { (byte) 65 }))
.hasSameContentAs(new ByteArrayInputStream(new byte[] { (byte) 66 }));
softly.assertThat(new Integer(20)).isEqualTo(new Integer(21));
softly.assertThat(22).isEqualTo(23);
softly.assertThat(new int[] { 24 }).isEqualTo(new int[] { 25 });
softly.assertThat((Iterable<String>) Lists.newArrayList("26")).isEqualTo(Lists.newArrayList("27"));
softly.assertThat(Lists.newArrayList("28").iterator()).contains("29");
softly.assertThat(Lists.newArrayList("30")).isEqualTo(Lists.newArrayList("31"));
softly.assertThat(new Long(32L)).isEqualTo(new Long(33L));
softly.assertThat(34L).isEqualTo(35L);
softly.assertThat(new long[] { 36L }).isEqualTo(new long[] { 37L });
softly.assertThat(Maps.mapOf(MapEntry.entry("38", "39"))).isEqualTo(Maps.mapOf(MapEntry.entry("40", "41")));
softly.assertThat(new Short((short) 42)).isEqualTo(new Short((short) 43));
softly.assertThat((short) 44).isEqualTo((short) 45);
softly.assertThat(new short[] { (short) 46 }).isEqualTo(new short[] { (short) 47 });
softly.assertThat("48").isEqualTo("49");
softly.assertThat(new Object() {
@Override
public String toString() {
return "50";
}
}).isEqualTo(new Object() {
@Override
public String toString() {
return "51";
}
});
softly.assertThat(new Object[] { new Object() {
@Override
public String toString() {
return "52";
}
} }).isEqualTo(new Object[] { new Object() {
@Override
public String toString() {
return "53";
}
} });
final IllegalArgumentException illegalArgumentException = new IllegalArgumentException("IllegalArgumentException message");
softly.assertThat(illegalArgumentException).hasMessage("NullPointerException message");
softly.assertThatThrownBy(new ThrowingCallable() {
@Override
public void call() throws Exception {
throw new Exception("something was wrong");
}
}).hasMessage("something was good");
softly.assertThat(Maps.mapOf(MapEntry.entry("54", "55"))).contains(MapEntry.entry("1", "2"));
softly.assertThat(new URI("http://assertj.org")).hasPort(8888);
softly.assertAll();
fail("Should not reach here");
} catch (SoftAssertionError e) {
List<String> errors = e.getErrors();
assertThat(errors).hasSize(41);
assertThat(errors.get(0)).isEqualTo("expected:<[1]> but was:<[0]>");
assertThat(errors.get(1)).isEqualTo("expected:<[tru]e> but was:<[fals]e>");
assertThat(errors.get(2)).isEqualTo("expected:<[tru]e> but was:<[fals]e>");
assertThat(errors.get(3)).isEqualTo("expected:<[[tru]e]> but was:<[[fals]e]>");
assertThat(errors.get(4)).isEqualTo("expected:<[1]> but was:<[0]>");
assertThat(errors.get(5)).isEqualTo("expected:<0x0[3]> but was:<0x0[2]>");
assertThat(errors.get(6)).isEqualTo("expected:<[[5]]> but was:<[[4]]>");
assertThat(errors.get(7)).isEqualTo("expected:<'[B]'> but was:<'[A]'>");
assertThat(errors.get(8)).isEqualTo("expected:<'[D]'> but was:<'[C]'>");
assertThat(errors.get(9)).isEqualTo("expected:<['[F]']> but was:<['[E]']>");
assertThat(errors.get(10)).isEqualTo("expected:<[b]> but was:<[a]>");
assertThat(errors.get(11)).isEqualTo("expected:<java.lang.[String]> but was:<java.lang.[Object]>");
assertThat(errors.get(12)).isEqualTo("expected:<[2000-01-01T00:00:01].000> but was:<[1999-12-31T23:59:59].000>");
assertThat(errors.get(13)).isEqualTo("expected:<[7].0> but was:<[6].0>");
assertThat(errors.get(14)).isEqualTo("expected:<[9].0> but was:<[8].0>");
assertThat(errors.get(15)).isEqualTo("expected:<[1[1].0]> but was:<[1[0].0]>");
assertThat(errors.get(16)).isEqualTo("expected:<File(b)> but was:<File(a)>");
assertThat(errors.get(17)).isEqualTo("expected:<1[3].0f> but was:<1[2].0f>");
assertThat(errors.get(18)).isEqualTo("expected:<1[5].0f> but was:<1[4].0f>");
assertThat(errors.get(19)).isEqualTo("expected:<[1[7].0f]> but was:<[1[6].0f]>");
assertThat(errors.get(20)).isEqualTo(String.format("%nInputStreams do not have same content:"
+ System.getProperty("line.separator")
+ "line:<1>, expected:<B> but was:<A>"));
assertThat(errors.get(21)).isEqualTo("expected:<2[1]> but was:<2[0]>");
assertThat(errors.get(22)).isEqualTo("expected:<2[3]> but was:<2[2]>");
assertThat(errors.get(23)).isEqualTo("expected:<[2[5]]> but was:<[2[4]]>");
assertThat(errors.get(24)).isEqualTo("expected:<[\"2[7]\"]> but was:<[\"2[6]\"]>");
assertThat(errors.get(25)).isEqualTo(String.format("%nExpecting:%n" +
" <[\"28\"]>%n" +
"to contain:%n" +
" <[\"29\"]>%n" +
"but could not find:%n" +
" <[\"29\"]>%n"));
assertThat(errors.get(26)).isEqualTo("expected:<[\"3[1]\"]> but was:<[\"3[0]\"]>");
assertThat(errors.get(27)).isEqualTo("expected:<3[3]L> but was:<3[2]L>");
assertThat(errors.get(28)).isEqualTo("expected:<3[5]L> but was:<3[4]L>");
assertThat(errors.get(29)).isEqualTo("expected:<[3[7]L]> but was:<[3[6]L]>");
assertThat(errors.get(30)).isEqualTo("expected:<{\"[40\"=\"41]\"}> but was:<{\"[38\"=\"39]\"}>");
assertThat(errors.get(31)).isEqualTo("expected:<4[3]> but was:<4[2]>");
assertThat(errors.get(32)).isEqualTo("expected:<4[5]> but was:<4[4]>");
assertThat(errors.get(33)).isEqualTo("expected:<[4[7]]> but was:<[4[6]]>");
assertThat(errors.get(34)).isEqualTo("expected:<\"4[9]\"> but was:<\"4[8]\">");
assertThat(errors.get(35)).isEqualTo("expected:<5[1]> but was:<5[0]>");
assertThat(errors.get(36)).isEqualTo("expected:<[5[3]]> but was:<[5[2]]>");
assertThat(errors.get(37)).isEqualTo(String.format("%nExpecting message:%n"
+ " <\"NullPointerException message\">%n"
+ "but was:%n"
+ " <\"IllegalArgumentException message\">"));
assertThat(errors.get(38)).isEqualTo(String.format("%nExpecting message:%n"
+ " <\"something was good\">%n"
+ "but was:%n"
+ " <\"something was wrong\">"));
assertThat(errors.get(39)).isEqualTo(String.format("%nExpecting:%n"
+ " <{\"54\"=\"55\"}>%n"
+ "to contain:%n"
+ " <[MapEntry[key=\"1\", value=\"2\"]]>%n"
+ "but could not find:%n"
+ " <[MapEntry[key=\"1\", value=\"2\"]]>%n"));
assertThat(errors.get(40)).contains("Expecting port of");
}
}
@Test
public void should_pass_when_using_extracting_with_list() {
List<Name> names = asList(name("John", "Doe"), name("Jane", "Doe"));
softly.assertThat(names)
.extracting("first")
.as("using extracting()")
.contains("John")
.contains("Jane");
softly.assertThat(names)
.extracting(new Extractor<Name, String>() {
@Override
public String extract(Name input) {
return input.getFirst();
}
})
.as("using extracting(Extractor)")
.contains("John")
.contains("Jane");
softly.assertThat(names)
.extracting("first", String.class)
.as("using extracting(..., Class)")
.contains("John")
.contains("Jane");
softly.assertThat(names)
.extracting("first", "last")
.as("using extracting(...)")
.contains(tuple("John", "Doe"))
.contains(tuple("Jane", "Doe"));
softly.assertThat(names)
.extractingResultOf("getFirst", String.class)
.as("using extractingResultOf(method, Class)")
.contains("John")
.contains("Jane");
softly.assertThat(names)
.extractingResultOf("getFirst")
.as("using extractingResultOf(method)")
.contains("John")
.contains("Jane");
softly.assertAll();
}
@Test
public void should_pass_when_using_extracting_with_iterable() {
Iterable<Name> names = asList(name("John", "Doe"), name("Jane", "Doe"));
try (AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions()) {
softly.assertThat(names)
.extracting("first")
.as("using extracting()")
.contains("John")
.contains("Jane");
softly.assertThat(names)
.extracting(new Extractor<Name, String>() {
@Override
public String extract(Name input) {
return input.getFirst();
}
})
.as("using extracting(Extractor)")
.contains("John")
.contains("Jane");
softly.assertThat(names)
.extracting("first", String.class)
.as("using extracting(..., Class)")
.contains("John")
.contains("Jane");
softly.assertThat(names)
.extracting("first", "last")
.as("using extracting(...)")
.contains(tuple("John", "Doe"))
.contains(tuple("Jane", "Doe"));
softly.assertThat(names)
.extractingResultOf("getFirst", String.class)
.as("using extractingResultOf(method, Class)")
.contains("John")
.contains("Jane");
softly.assertThat(names)
.extractingResultOf("getFirst")
.as("using extractingResultOf(method)")
.contains("John")
.contains("Jane");
}
}
@Test
public void should_pass_when_using_extracting_with_array() {
Name[] namesAsArray = new Name[] { name("John", "Doe"), name("Jane", "Doe") };
try (AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions()) {
softly.assertThat(namesAsArray)
.extracting("first")
.as("using extracting()")
.contains("John")
.contains("Jane");
softly.assertThat(namesAsArray)
.extracting(new Extractor<Name, String>() {
@Override
public String extract(Name input) {
return input.getFirst();
}
})
.as("using extracting(Extractor)")
.contains("John")
.contains("Jane");
softly.assertThat(namesAsArray)
.extracting("first", String.class)
.as("using extracting(..., Class)")
.contains("John")
.contains("Jane");
softly.assertThat(namesAsArray)
.extracting("first", "last")
.as("using extracting(...)")
.contains(tuple("John", "Doe"))
.contains(tuple("Jane", "Doe"));
softly.assertThat(namesAsArray)
.extractingResultOf("getFirst", String.class)
.as("using extractingResultOf(method, Class)")
.contains("John")
.contains("Jane");
softly.assertThat(namesAsArray)
.extractingResultOf("getFirst")
.as("using extractingResultOf(method)")
.contains("John")
.contains("Jane");
}
}
@Test
public void should_pass_when_using_extracting_with_iterator() {
Iterator<Name> names = asList(name("John", "Doe"), name("Jane", "Doe")).iterator();
try (AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions()) {
softly.assertThat(names)
.extracting("first")
.as("using extracting()")
.contains("John")
.contains("Jane");
}
}
@Test
public void should_pass_when_using_flat_extracting() {
List<CartoonCharacter> characters = asList(homer, fred);
softly.assertThat(characters)
.flatExtracting(children())
.as("using flatExtracting on Iterable")
.hasSize(4);
CartoonCharacter[] charactersAsArray = characters.toArray(new CartoonCharacter[characters.size()]);
softly.assertThat(charactersAsArray)
.flatExtracting(children())
.as("using flatExtracting on array")
.hasSize(4);
softly.assertAll();
}
@Test
public void should_collect_all_errors_when_using_extracting() {
List<Name> names = asList(name("John", "Doe"), name("Jane", "Doe"));
softly.assertThat(names)
.extracting("first")
.overridingErrorMessage("error 1")
.contains("gandalf")
.overridingErrorMessage("error 2")
.contains("frodo");
softly.assertThat(names)
.extracting("last")
.overridingErrorMessage("error 3")
.isEmpty();
try {
softly.assertAll();
shouldHaveThrown(SoftAssertionError.class);
} catch (SoftAssertionError e) {
assertThat(e.getErrors()).containsExactly("error 1", "error 2", "error 3");
}
}
@Test
public void should_collect_all_errors_when_using_flat_extracting() throws Exception {
List<CartoonCharacter> characters = asList(homer, fred);
softly.assertThat(characters)
.flatExtracting(children())
.overridingErrorMessage("error 1")
.hasSize(0)
.overridingErrorMessage("error 2")
.isEmpty();
try {
softly.assertAll();
shouldHaveThrown(SoftAssertionError.class);
} catch (SoftAssertionError e) {
assertThat(e.getErrors()).containsExactly("error 1", "error 2");
}
}
@Test
public void should_collect_all_errors_when_using_filtering() throws Exception {
softly.assertThat(asList(homer, fred))
.filteredOn("name", "Homer Simpson")
.hasSize(10)
.isEmpty();
try {
softly.assertAll();
shouldHaveThrown(SoftAssertionError.class);
} catch (SoftAssertionError e) {
assertThat(e.getErrors()).containsOnly(format("%nExpected size:<10> but was:<1> in:%n<[CartoonCharacter [name=Homer Simpson]]>"),
format("%nExpecting empty but was:<[CartoonCharacter [name=Homer Simpson]]>"));
}
}
private static Name name(String first, String last) {
return new Name(first, last);
}
private static ChildrenExtractor children() {
return new ChildrenExtractor();
}
private static class ChildrenExtractor implements Extractor<CartoonCharacter, Collection<CartoonCharacter>> {
@Override
public Collection<CartoonCharacter> extract(CartoonCharacter input) {
return input.getChildren();
}
}
}