1010
1111package org .junit .jupiter .api ;
1212
13+ import static org .junit .jupiter .api .Assertions .assertEquals ;
14+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
15+
1316import java .io .Serializable ;
17+ import java .util .List ;
1418import java .util .Objects ;
1519
16- import org .apache .groovy .parser .antlr4 .util .StringUtils ;
1720import org .jspecify .annotations .Nullable ;
1821import org .opentest4j .AssertionFailedError ;
22+ import org .opentest4j .MultipleFailuresError ;
1923import org .opentest4j .ValueWrapper ;
2024
21- class AssertionTestUtils {
25+ public class AssertionTestUtils {
2226
2327 private AssertionTestUtils () {
2428 /* no-op */
2529 }
2630
27- static void expectAssertionFailedError () {
31+ public static void expectAssertionFailedError () {
2832 throw new AssertionError ("Should have thrown an " + AssertionFailedError .class .getName ());
2933 }
3034
31- static void assertEmptyMessage (Throwable ex ) throws AssertionError {
32- if (!StringUtils . isEmpty ( ex .getMessage ())) {
35+ public static void assertEmptyMessage (Throwable ex ) throws AssertionError {
36+ if (!( ex . getMessage () == null || ex .getMessage (). isEmpty ())) {
3337 throw new AssertionError ("Exception message should be empty, but was [" + ex .getMessage () + "]." );
3438 }
3539 }
3640
37- static void assertMessageEquals (Throwable ex , String msg ) throws AssertionError {
41+ public static void assertMessageEquals (Throwable ex , String msg ) throws AssertionError {
3842 if (!msg .equals (ex .getMessage ())) {
3943 throw new AssertionError ("Exception message should be [" + msg + "], but was [" + ex .getMessage () + "]." );
4044 }
4145 }
4246
43- static void assertMessageMatches (Throwable ex , String regex ) throws AssertionError {
47+ public static void assertMessageMatches (Throwable ex , String regex ) throws AssertionError {
4448 if (ex .getMessage () == null || !ex .getMessage ().matches (regex )) {
4549 throw new AssertionError ("Exception message should match regular expression [" + regex + "], but was ["
4650 + ex .getMessage () + "]." );
4751 }
4852 }
4953
50- static void assertMessageStartsWith (@ Nullable Throwable ex , String msg ) throws AssertionError {
54+ public static void assertMessageStartsWith (@ Nullable Throwable ex , String msg ) throws AssertionError {
5155 if (ex == null ) {
5256 throw new AssertionError ("Cause should not have been null" );
5357 }
@@ -57,14 +61,14 @@ static void assertMessageStartsWith(@Nullable Throwable ex, String msg) throws A
5761 }
5862 }
5963
60- static void assertMessageEndsWith (Throwable ex , String msg ) throws AssertionError {
64+ public static void assertMessageEndsWith (Throwable ex , String msg ) throws AssertionError {
6165 if (ex .getMessage () == null || !ex .getMessage ().endsWith (msg )) {
6266 throw new AssertionError (
6367 "Exception message should end with [" + msg + "], but was [" + ex .getMessage () + "]." );
6468 }
6569 }
6670
67- static void assertMessageContains (@ Nullable Throwable ex , String msg ) throws AssertionError {
71+ public static void assertMessageContains (@ Nullable Throwable ex , String msg ) throws AssertionError {
6872 if (ex == null ) {
6973 throw new AssertionError ("Cause should not have been null" );
7074 }
@@ -74,7 +78,7 @@ static void assertMessageContains(@Nullable Throwable ex, String msg) throws Ass
7478 }
7579 }
7680
77- static void assertExpectedAndActualValues (AssertionFailedError ex , @ Nullable Object expected ,
81+ public static void assertExpectedAndActualValues (AssertionFailedError ex , @ Nullable Object expected ,
7882 @ Nullable Object actual ) throws AssertionError {
7983 if (!wrapsEqualValue (ex .getExpected (), expected )) {
8084 throw new AssertionError ("Expected value in AssertionFailedError should equal ["
@@ -86,7 +90,7 @@ static void assertExpectedAndActualValues(AssertionFailedError ex, @Nullable Obj
8690 }
8791 }
8892
89- static boolean wrapsEqualValue (ValueWrapper wrapper , @ Nullable Object value ) {
93+ public static boolean wrapsEqualValue (ValueWrapper wrapper , @ Nullable Object value ) {
9094 if (value == null || value instanceof Serializable ) {
9195 return Objects .equals (value , wrapper .getValue ());
9296 }
@@ -95,14 +99,32 @@ static boolean wrapsEqualValue(ValueWrapper wrapper, @Nullable Object value) {
9599 && Objects .equals (wrapper .getType (), value .getClass ());
96100 }
97101
98- static void recurseIndefinitely () {
102+ public static void recurseIndefinitely () {
99103 // simulate infinite recursion
100104 throw new StackOverflowError ();
101105 }
102106
103- static void runOutOfMemory () {
107+ public static void runOutOfMemory () {
104108 // simulate running out of memory
105109 throw new OutOfMemoryError ("boom" );
106110 }
107111
112+ @ SafeVarargs
113+ public static void assertExpectedExceptionTypes (MultipleFailuresError multipleFailuresError ,
114+ Class <? extends Throwable >... exceptionTypes ) {
115+
116+ assertNotNull (multipleFailuresError , "MultipleFailuresError" );
117+ List <Throwable > failures = multipleFailuresError .getFailures ();
118+ assertEquals (exceptionTypes .length , failures .size (), "number of failures" );
119+
120+ // Verify that exceptions are also present as suppressed exceptions.
121+ // https://github.com/junit-team/junit-framework/issues/1602
122+ Throwable [] suppressed = multipleFailuresError .getSuppressed ();
123+ assertEquals (exceptionTypes .length , suppressed .length , "number of suppressed exceptions" );
124+
125+ for (int i = 0 ; i < exceptionTypes .length ; i ++) {
126+ assertEquals (exceptionTypes [i ], failures .get (i ).getClass (), "exception type [" + i + "]" );
127+ assertEquals (exceptionTypes [i ], suppressed [i ].getClass (), "suppressed exception type [" + i + "]" );
128+ }
129+ }
108130}
0 commit comments