/
AssertionsForClassTypes.java
1959 lines (1822 loc) · 73.8 KB
/
AssertionsForClassTypes.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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-2022 the original author or authors.
*/
package org.assertj.core.api;
import static org.assertj.core.data.Percentage.withPercentage;
import java.io.File;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.text.DateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Matcher;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.assertj.core.api.filter.FilterOperator;
import org.assertj.core.api.filter.Filters;
import org.assertj.core.api.filter.InFilter;
import org.assertj.core.api.filter.NotFilter;
import org.assertj.core.api.filter.NotInFilter;
import org.assertj.core.condition.AllOf;
import org.assertj.core.condition.AnyOf;
import org.assertj.core.condition.DoesNotHave;
import org.assertj.core.condition.Not;
import org.assertj.core.data.Index;
import org.assertj.core.data.MapEntry;
import org.assertj.core.data.Offset;
import org.assertj.core.data.Percentage;
import org.assertj.core.groups.Properties;
import org.assertj.core.groups.Tuple;
import org.assertj.core.presentation.StandardRepresentation;
import org.assertj.core.util.CanIgnoreReturnValue;
import org.assertj.core.util.CheckReturnValue;
import org.assertj.core.util.Files;
import org.assertj.core.util.Paths;
import org.assertj.core.util.URLs;
import org.assertj.core.util.introspection.FieldSupport;
/**
* Java 8 is picky when choosing the right <code>assertThat</code> method if the object under test is generic and bounded,
* for example if foo is instance of T that extends Exception, java 8 will complain that it can't resolve
* the proper <code>assertThat</code> method (normally <code>assertThat(Throwable)</code> as foo might implement an interface like List,
* if that occurred <code>assertThat(List)</code> would also be a possible choice - thus confusing java 8.
* <p>
* This why {@link Assertions} have been split in {@link AssertionsForClassTypes} and {@link AssertionsForInterfaceTypes}
* (see http://stackoverflow.com/questions/29499847/ambiguous-method-in-java-8-why).
*/
@CheckReturnValue
public class AssertionsForClassTypes {
/**
* Create assertion for {@link java.util.concurrent.CompletableFuture}.
*
* @param actual the actual value.
* @param <RESULT> the type of the value contained in the {@link java.util.concurrent.CompletableFuture}.
*
* @return the created assertion object.
*/
public static <RESULT> CompletableFutureAssert<RESULT> assertThat(CompletableFuture<RESULT> actual) {
return new CompletableFutureAssert<>(actual);
}
/**
* Create assertion for {@link java.util.Optional}.
*
* @param actual the actual value.
* @param <VALUE> the type of the value contained in the {@link java.util.Optional}.
*
* @return the created assertion object.
*/
public static <VALUE> OptionalAssert<VALUE> assertThat(Optional<VALUE> actual) {
return new OptionalAssert<>(actual);
}
/**
* Create assertion for {@link java.util.OptionalDouble}.
*
* @param actual the actual value.
*
* @return the created assertion object.
*/
public static OptionalDoubleAssert assertThat(OptionalDouble actual) {
return new OptionalDoubleAssert(actual);
}
/**
* Create assertion for {@link java.util.OptionalInt}.
*
* @param actual the actual value.
*
* @return the created assertion object.
*/
public static OptionalIntAssert assertThat(OptionalInt actual) {
return new OptionalIntAssert(actual);
}
/**
* Create assertion for {@link java.util.regex.Matcher}
*
* @param actual the actual value
*
* @return the created assertion object
*/
public static MatcherAssert assertThat(Matcher actual) {
return new MatcherAssert(actual);
}
/**
* Create assertion for {@link java.util.OptionalInt}.
*
* @param actual the actual value.
*
* @return the created assertion object.
*/
public static OptionalLongAssert assertThat(OptionalLong actual) {
return new OptionalLongAssert(actual);
}
/**
* Creates a new instance of <code>{@link BigDecimalAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractBigDecimalAssert<?> assertThat(BigDecimal actual) {
return new BigDecimalAssert(actual);
}
/**
* Creates a new instance of <code>{@link UriAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractUriAssert<?> assertThat(URI actual) {
return new UriAssert(actual);
}
/**
* Creates a new instance of <code>{@link UrlAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractUrlAssert<?> assertThat(URL actual) {
return new UrlAssert(actual);
}
/**
* Creates a new instance of <code>{@link BooleanAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractBooleanAssert<?> assertThat(boolean actual) {
return new BooleanAssert(actual);
}
/**
* Creates a new instance of <code>{@link BooleanAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractBooleanAssert<?> assertThat(Boolean actual) {
return new BooleanAssert(actual);
}
/**
* Creates a new instance of <code>{@link BooleanArrayAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractBooleanArrayAssert<?> assertThat(boolean[] actual) {
return new BooleanArrayAssert(actual);
}
/**
* Creates a new instance of <code>{@link Boolean2DArrayAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.17.0
*/
public static Boolean2DArrayAssert assertThat(boolean[][] actual) {
return new Boolean2DArrayAssert(actual);
}
/**
* Creates a new instance of <code>{@link ByteAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractByteAssert<?> assertThat(byte actual) {
return new ByteAssert(actual);
}
/**
* Creates a new instance of <code>{@link ByteAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractByteAssert<?> assertThat(Byte actual) {
return new ByteAssert(actual);
}
/**
* Creates a new instance of <code>{@link ByteArrayAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractByteArrayAssert<?> assertThat(byte[] actual) {
return new ByteArrayAssert(actual);
}
/**
* Creates a new instance of <code>{@link Byte2DArrayAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.17.0
*/
public static Byte2DArrayAssert assertThat(byte[][] actual) {
return new Byte2DArrayAssert(actual);
}
/**
* Creates a new instance of <code>{@link CharacterAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractCharacterAssert<?> assertThat(char actual) {
return new CharacterAssert(actual);
}
/**
* Creates a new instance of <code>{@link CharArrayAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractCharArrayAssert<?> assertThat(char[] actual) {
return new CharArrayAssert(actual);
}
/**
* Creates a new instance of <code>{@link Char2DArrayAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.17.0
*/
public static Char2DArrayAssert assertThat(char[][] actual) {
return new Char2DArrayAssert(actual);
}
/**
* Creates a new instance of <code>{@link CharacterAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractCharacterAssert<?> assertThat(Character actual) {
return new CharacterAssert(actual);
}
/**
* Creates a new instance of <code>{@link ClassAssert}</code>
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static ClassAssert assertThat(Class<?> actual) {
return new ClassAssert(actual);
}
/**
* Creates a new instance of <code>{@link DoubleAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractDoubleAssert<?> assertThat(double actual) {
return new DoubleAssert(actual);
}
/**
* Creates a new instance of <code>{@link DoubleAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractDoubleAssert<?> assertThat(Double actual) {
return new DoubleAssert(actual);
}
/**
* Creates a new instance of <code>{@link DoubleArrayAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractDoubleArrayAssert<?> assertThat(double[] actual) {
return new DoubleArrayAssert(actual);
}
/**
* Creates a new instance of <code>{@link Double2DArrayAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.17.0
*/
public static Double2DArrayAssert assertThat(double[][] actual) {
return new Double2DArrayAssert(actual);
}
/**
* Creates a new instance of <code>{@link FileAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractFileAssert<?> assertThat(File actual) {
return new FileAssert(actual);
}
/**
* Creates a new instance of <code>{@link InputStreamAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractInputStreamAssert<?, ? extends InputStream> assertThat(InputStream actual) {
return new InputStreamAssert(actual);
}
/**
* Creates a new instance of <code>{@link FloatAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractFloatAssert<?> assertThat(float actual) {
return new FloatAssert(actual);
}
/**
* Creates a new instance of <code>{@link FloatAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractFloatAssert<?> assertThat(Float actual) {
return new FloatAssert(actual);
}
/**
* Creates a new instance of <code>{@link FloatArrayAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractFloatArrayAssert<?> assertThat(float[] actual) {
return new FloatArrayAssert(actual);
}
/**
* Creates a new instance of <code>{@link Float2DArrayAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.17.0
*/
public static Float2DArrayAssert assertThat(float[][] actual) {
return new Float2DArrayAssert(actual);
}
/**
* Creates a new instance of <code>{@link IntegerAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractIntegerAssert<?> assertThat(int actual) {
return new IntegerAssert(actual);
}
/**
* Creates a new instance of <code>{@link IntArrayAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractIntArrayAssert<?> assertThat(int[] actual) {
return new IntArrayAssert(actual);
}
/**
* Creates a new instance of <code>{@link Int2DArrayAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.17.0
*/
public static Int2DArrayAssert assertThat(int[][] actual) {
return new Int2DArrayAssert(actual);
}
/**
* Creates a new instance of <code>{@link IntegerAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractIntegerAssert<?> assertThat(Integer actual) {
return new IntegerAssert(actual);
}
/**
* Creates a new instance of <code>{@link LongAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractLongAssert<?> assertThat(long actual) {
return new LongAssert(actual);
}
/**
* Creates a new instance of <code>{@link LongAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractLongAssert<?> assertThat(Long actual) {
return new LongAssert(actual);
}
/**
* Creates a new instance of <code>{@link LongArrayAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractLongArrayAssert<?> assertThat(long[] actual) {
return new LongArrayAssert(actual);
}
/**
* Creates a new instance of <code>{@link Long2DArrayAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.17.0
*/
public static Long2DArrayAssert assertThat(long[][] actual) {
return new Long2DArrayAssert(actual);
}
/**
* Creates a new instance of <code>{@link ObjectAssert}</code>.
*
* @param <T> the actual value type.
* @param actual the actual value.
* @return the created assertion object.
*/
public static <T> ObjectAssert<T> assertThat(T actual) {
return new ObjectAssert<>(actual);
}
/**
* Creates a new instance of <code>{@link ObjectArrayAssert}</code>.
*
* @param <T> the actual elements type.
* @param actual the actual value.
* @return the created assertion object.
*/
public static <T> ObjectArrayAssert<T> assertThat(T[] actual) {
return new ObjectArrayAssert<>(actual);
}
/**
* Creates a new instance of <code>{@link Object2DArrayAssert}</code>.
*
* @param <T> the actual elements type.
* @param actual the actual value.
* @return the created assertion object.
* @since 3.17.0
*/
public static <T> Object2DArrayAssert<T> assertThat(T[][] actual) {
return new Object2DArrayAssert<>(actual);
}
/**
* Creates a new instance of <code>{@link ShortAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractShortAssert<?> assertThat(short actual) {
return new ShortAssert(actual);
}
/**
* Creates a new instance of <code>{@link ShortAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractShortAssert<?> assertThat(Short actual) {
return new ShortAssert(actual);
}
/**
* Creates a new instance of <code>{@link ShortArrayAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractShortArrayAssert<?> assertThat(short[] actual) {
return new ShortArrayAssert(actual);
}
/**
* Creates a new instance of <code>{@link Short2DArrayAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.17.0
*/
public static Short2DArrayAssert assertThat(short[][] actual) {
return new Short2DArrayAssert(actual);
}
/**
* Creates a new instance of <code>{@link CharSequenceAssert}</code> from a {@link StringBuilder}.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.11.0
*/
public static AbstractCharSequenceAssert<?, ? extends CharSequence> assertThat(StringBuilder actual) {
return new CharSequenceAssert(actual);
}
/**
* Creates a new instance of <code>{@link CharSequenceAssert}</code> from a {@link StringBuffer}.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.11.0
*/
public static AbstractCharSequenceAssert<?, ? extends CharSequence> assertThat(StringBuffer actual) {
return new CharSequenceAssert(actual);
}
/**
* Creates a new instance of <code>{@link StringAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractStringAssert<?> assertThat(String actual) {
return new StringAssert(actual);
}
/**
* Creates a new instance of <code>{@link DateAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractDateAssert<?> assertThat(Date actual) {
return new DateAssert(actual);
}
/**
* Creates a new instance of <code>{@link ZonedDateTimeAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractZonedDateTimeAssert<?> assertThat(ZonedDateTime actual) {
return new ZonedDateTimeAssert(actual);
}
/**
* Creates a new instance of <code>{@link LocalDateTimeAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractLocalDateTimeAssert<?> assertThat(LocalDateTime actual) {
return new LocalDateTimeAssert(actual);
}
/**
* Creates a new instance of <code>{@link java.time.OffsetDateTime}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractOffsetDateTimeAssert<?> assertThat(OffsetDateTime actual) {
return new OffsetDateTimeAssert(actual);
}
/**
* Create assertion for {@link java.time.OffsetTime}.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractOffsetTimeAssert<?> assertThat(OffsetTime actual) {
return new OffsetTimeAssert(actual);
}
/**
* Creates a new instance of <code>{@link LocalTimeAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static AbstractLocalTimeAssert<?> assertThat(LocalTime actual) {
return new LocalTimeAssert(actual);
}
/**
* Creates a new instance of <code>{@link LocalDateAssert}</code>.
*
* @param localDate the actual value.
* @return the created assertion object.
*/
public static AbstractLocalDateAssert<?> assertThat(LocalDate localDate) {
return new LocalDateAssert(localDate);
}
/**
* Creates a new instance of <code>{@link InstantAssert}</code>.
*
* @param instant the actual value.
* @return the created assertion object.
* @since 3.7.0
*/
public static AbstractInstantAssert<?> assertThat(Instant instant) {
return new InstantAssert(instant);
}
/**
* Creates a new instance of <code>{@link DurationAssert}</code>.
*
* @param duration the actual value.
* @return the created assertion object.
* @since 3.15.0
*/
public static AbstractDurationAssert<?> assertThat(Duration duration) {
return new DurationAssert(duration);
}
/**
* Creates a new instance of <code>{@link PeriodAssert}</code>.
*
* @param period the actual value.
* @return the created assertion object.
* @since 3.17.0
*/
public static AbstractPeriodAssert<?> assertThat(Period period) {
return new PeriodAssert(period);
}
/**
* Creates a new instance of <code>{@link ThrowableAssert}</code>.
*
* @param <T> the type of the actual throwable.
* @param actual the actual value.
* @return the created {@link ThrowableAssert}.
*/
public static <T extends Throwable> AbstractThrowableAssert<?, T> assertThat(T actual) {
return new ThrowableAssert<>(actual);
}
/**
* Allows to capture and then assert on a {@link Throwable} (easier done with lambdas).
* <p>
* Java 8 example :
* <pre><code class='java'> {@literal @}Test
* public void testException() {
* assertThatThrownBy(() -> { throw new Exception("boom!") }).isInstanceOf(Exception.class)
* .hasMessageContaining("boom");
* }</code></pre>
*
* If the provided {@link ThrowingCallable} does not raise an exception, an error is immediately thrown,
* in that case the test description provided with {@link AbstractAssert#as(String, Object...) as(String, Object...)} is not honored.<br>
* To use a test description, use {@link #catchThrowable(ThrowableAssert.ThrowingCallable)} as shown below:
* <pre><code class='java'> // assertion will fail but "display me" won't appear in the error
* assertThatThrownBy(() -> {}).as("display me")
* .isInstanceOf(Exception.class);
*
* // assertion will fail AND "display me" will appear in the error
* Throwable thrown = catchThrowable(() -> {});
* assertThat(thrown).as("display me")
* .isInstanceOf(Exception.class);</code></pre>
*
* Alternatively you can also use <code>assertThatCode(ThrowingCallable)</code> for the test description provided
* with {@link AbstractAssert#as(String, Object...) as(String, Object...)} to always be honored.
*
* @param shouldRaiseThrowable The {@link ThrowingCallable} or lambda with the code that should raise the throwable.
* @return the created {@link ThrowableAssert}.
*/
@CanIgnoreReturnValue
public static AbstractThrowableAssert<?, ? extends Throwable> assertThatThrownBy(ThrowingCallable shouldRaiseThrowable) {
return assertThat(catchThrowable(shouldRaiseThrowable)).hasBeenThrown();
}
/**
* Allows to capture and then assert on a {@link Throwable} like {@code assertThatThrownBy(ThrowingCallable)} but this method
* let you set the assertion description the same way you do with {@link AbstractAssert#as(String, Object...) as(String, Object...)}.
* <p>
* Example:
* <pre><code class='java'> {@literal @}Test
* public void testException() {
* // if this assertion failed (but it doesn't), the error message would start with [Test explosive code]
* assertThatThrownBy(() -> { throw new IOException("boom!") }, "Test explosive code")
* .isInstanceOf(IOException.class)
* .hasMessageContaining("boom");
* }</code></pre>
*
* If the provided {@link ThrowingCallable ThrowingCallable} does not raise an exception, an error is immediately thrown.
* <p>
* The test description provided is honored but not the one with {@link AbstractAssert#as(String, Object...) as(String, Object...)}, example:
* <pre><code class='java'> // assertion will fail but "display me" won't appear in the error message
* assertThatThrownBy(() -> {}).as("display me")
* .isInstanceOf(Exception.class);
*
* // assertion will fail AND "display me" will appear in the error message
* assertThatThrownBy(() -> {}, "display me")
* .isInstanceOf(Exception.class);</code></pre>
*
* @param shouldRaiseThrowable The {@link ThrowingCallable} or lambda with the code that should raise the throwable.
* @param description the new description to set.
* @param args optional parameter if description is a format String.
*
* @return the created {@link ThrowableAssert}.
*
* @since 3.9.0
*/
@CanIgnoreReturnValue
public static AbstractThrowableAssert<?, ? extends Throwable> assertThatThrownBy(ThrowingCallable shouldRaiseThrowable,
String description, Object... args) {
return assertThat(catchThrowable(shouldRaiseThrowable)).as(description, args).hasBeenThrown();
}
/**
* Entry point to check that an exception of type T is thrown by a given {@code throwingCallable}
* which allows to chain assertions on the thrown exception.
* <p>
* Example:
* <pre><code class='java'> assertThatExceptionOfType(IOException.class)
* .isThrownBy(() -> { throw new IOException("boom!"); })
* .withMessage("boom!"); </code></pre>
*
* This method is more or less the same of {@link #assertThatThrownBy(ThrowableAssert.ThrowingCallable)} but in a more natural way.
*
* @param <T> the exception type.
* @param exceptionType the class of exception type.
* @return the created {@link ThrowableTypeAssert}.
*/
public static <T extends Throwable> ThrowableTypeAssert<T> assertThatExceptionOfType(final Class<? extends T> exceptionType) {
return new ThrowableTypeAssert<>(exceptionType);
}
/**
* Entry point to check that no exception of any type is thrown by a given {@code throwingCallable}.
* <p>
* Example:
* <pre><code class='java'>assertThatNoException().isThrownBy(() -> { System.out.println("OK"); });</code></pre>
*
* This method is more or less the same of {@code assertThatCode(...).doesNotThrowAnyException();} but in a more natural way.
* @return the created {@link NotThrownAssert}.
* @since 3.17.0
*/
public static NotThrownAssert assertThatNoException() {
return new NotThrownAssert();
}
/**
* Allows to capture and then assert on a {@link Throwable} more easily when used with Java 8 lambdas.
*
* <p>
* Example :
* </p>
*
* <pre><code class='java'> ThrowingCallable callable = () -> {
* throw new Exception("boom!");
* };
*
* // assertion succeeds
* assertThatCode(callable).isInstanceOf(Exception.class)
* .hasMessageContaining("boom");
*
* // assertion fails
* assertThatCode(callable).doesNotThrowAnyException();</code></pre>
*
* If the provided {@link ThrowingCallable} does not validate against next assertions, an error is immediately raised,
* in that case the test description provided with {@link AbstractAssert#as(String, Object...) as(String, Object...)} is not honored.<br>
* To use a test description, use {@link #catchThrowable(ThrowableAssert.ThrowingCallable)} as shown below.
*
* <pre><code class='java'> ThrowingCallable doNothing = () -> {
* // do nothing
* };
*
* // assertion fails and "display me" appears in the assertion error
* assertThatCode(doNothing).as("display me")
* .isInstanceOf(Exception.class);
*
* // assertion will fail AND "display me" will appear in the error
* Throwable thrown = catchThrowable(doNothing);
* assertThatCode(thrown).as("display me")
* .isInstanceOf(Exception.class); </code></pre>
* <p>
* This method was not named {@code assertThat} because the java compiler reported it ambiguous when used directly with a lambda :(
*
* @param shouldRaiseOrNotThrowable The {@link ThrowingCallable} or lambda with the code that should raise the throwable.
* @return The captured exception or <code>null</code> if none was raised by the callable.
* @since 3.7.0
*/
public static AbstractThrowableAssert<?, ? extends Throwable> assertThatCode(ThrowingCallable shouldRaiseOrNotThrowable) {
return assertThat(catchThrowable(shouldRaiseOrNotThrowable));
}
/**
* Allows catching a {@link Throwable} more easily when used with Java 8 lambdas.
*
* <p>
* This caught {@link Throwable} can then be asserted.
* </p>
*
* <p>
* Example:
* </p>
*
* <pre><code class='java'>{@literal @}Test
* public void testException() {
* // when
* Throwable thrown = catchThrowable(() -> { throw new Exception("boom!"); });
*
* // then
* assertThat(thrown).isInstanceOf(Exception.class)
* .hasMessageContaining("boom");
* } </code></pre>
*
* @param shouldRaiseThrowable The lambda with the code that should raise the exception.
* @return The captured exception or <code>null</code> if none was raised by the callable.
* @see AssertionsForClassTypes#catchThrowableOfType(ThrowableAssert.ThrowingCallable, Class)
*/
public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
return ThrowableAssert.catchThrowable(shouldRaiseThrowable);
}
/**
* Allows catching a {@link Throwable} of a specific type.
* <p>
* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown {@code catchThrowableOfType} returns null,
* otherwise it checks that the caught {@link Throwable} has the specified type then casts it to it before returning it,
* making it convenient to perform subtype-specific assertions on the result.
* <p>
* Example:
* <pre><code class='java'> class CustomParseException extends Exception {
* int line;
* int column;
*
* public CustomParseException(String msg, int l, int c) {
* super(msg);
* line = l;
* column = c;
* }
* }
*
* CustomParseException e = catchThrowableOfType(() -> { throw new CustomParseException("boom!", 1, 5); },
* CustomParseException.class);
* // assertions pass
* assertThat(e).hasMessageContaining("boom");
* assertThat(e.line).isEqualTo(1);
* assertThat(e.column).isEqualTo(5);
*
* // fails as CustomParseException is not a RuntimeException
* catchThrowableOfType(() -> { throw new CustomParseException("boom!", 1, 5); },
* RuntimeException.class);</code></pre>
*
* @param <THROWABLE> the {@link Throwable} type.
* @param shouldRaiseThrowable The lambda with the code that should raise the exception.
* @param type The type of exception that the code is expected to raise.
* @return The captured exception or <code>null</code> if none was raised by the callable.
* @see #catchThrowable(ThrowableAssert.ThrowingCallable)
* @since 3.9.0
*/
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable,
Class<THROWABLE> type) {
return ThrowableAssert.catchThrowableOfType(shouldRaiseThrowable, type);
}
// -------------------------------------------------------------------------------------------------
// fail methods : not assertions but here to have a single entry point to all AssertJ features.
// -------------------------------------------------------------------------------------------------
/**
* Only delegate to {@link Fail#setRemoveAssertJRelatedElementsFromStackTrace(boolean)} so that Assertions offers a
* full feature entry point to all AssertJ Assert features (but you can use {@link Fail} if you prefer).
*
* @param removeAssertJRelatedElementsFromStackTrace flag.
*/
public static void setRemoveAssertJRelatedElementsFromStackTrace(boolean removeAssertJRelatedElementsFromStackTrace) {
Fail.setRemoveAssertJRelatedElementsFromStackTrace(removeAssertJRelatedElementsFromStackTrace);
}
/**
* Only delegate to {@link Fail#fail(String)} so that Assertions offers a full feature entry point to all Assertj
* Assert features (but you can use Fail if you prefer).
*
* @param failureMessage error message.
* @throws AssertionError with the given message.
*/
public static void fail(String failureMessage) {
Fail.fail(failureMessage);
}
/**
* Only delegate to {@link Fail#fail(String, Throwable)} so that Assertions offers a full feature entry point to all
* AssertJ features (but you can use Fail if you prefer).
*
* @param failureMessage the description of the failed assertion. It can be {@code null}.
* @param realCause cause of the error.
* @throws AssertionError with the given message and with the {@link Throwable} that caused the failure.
*/
public static void fail(String failureMessage, Throwable realCause) {
Fail.fail(failureMessage, realCause);
}
/**
* Only delegate to {@link Fail#failBecauseExceptionWasNotThrown(Class)} so that Assertions offers a full feature
* entry point to all AssertJ features (but you can use Fail if you prefer).
*
* {@link Assertions#shouldHaveThrown(Class)} can be used as a replacement.
*
* @param throwableClass the Throwable class that was expected to be thrown.
* @throws AssertionError with a message explaining that a {@link Throwable} of given class was expected to be thrown but had
* not been.
*/
public static void failBecauseExceptionWasNotThrown(Class<? extends Throwable> throwableClass) {
Fail.shouldHaveThrown(throwableClass);
}
/**
* Only delegate to {@link Fail#shouldHaveThrown(Class)} so that Assertions offers a full feature
* entry point to all AssertJ features (but you can use Fail if you prefer).
*
* @param throwableClass the Throwable class that was expected to be thrown.
* @throws AssertionError with a message explaining that a {@link Throwable} of given class was expected to be thrown but had
* not been.
*/
public static void shouldHaveThrown(Class<? extends Throwable> throwableClass) {
Fail.shouldHaveThrown(throwableClass);
}
/**