2121
2222import org .junit .jupiter .api .Test ;
2323
24+ import java .io .IOException ;
25+
2426import static org .assertj .core .api .Assertions .assertThat ;
2527
2628/** Test for {@link ExceptionUtils}. */
@@ -31,52 +33,6 @@ void testGetExceptionMessage_nullThrowable() {
3133 assertThat (ExceptionUtils .getExceptionMessage (null )).isNull ();
3234 }
3335
34- @ Test
35- void testGetExceptionMessage_simpleException () {
36- var ex = new RuntimeException ("My Exception" );
37- assertThat (ExceptionUtils .getExceptionMessage (ex )).isEqualTo ("My Exception" );
38- }
39-
40- @ Test
41- void testGetExceptionMessage_simpleExceptionNullCause () {
42- var ex = new OutOfMemoryError (null );
43- assertThat (ExceptionUtils .getExceptionMessage (ex )).isEqualTo ("OutOfMemoryError" );
44- }
45-
46- @ Test
47- void testGetExceptionMessage_exceptionWithCause () {
48- var ex = new RuntimeException ("Root Cause" );
49- var exWrapped = new RuntimeException ("Wrapped Exception" , ex );
50- assertThat (ExceptionUtils .getExceptionMessage (exWrapped ))
51- .isEqualTo ("Wrapped Exception -> Root Cause" );
52- }
53-
54- @ Test
55- void testGetExceptionMessage_exactlyThree () {
56- var ex3 = new IllegalAccessException (null );
57- var ex2 = new RuntimeException ("Cause 2" , ex3 );
58- var ex = new RuntimeException ("Cause 1" , ex2 );
59- assertThat (ExceptionUtils .getExceptionMessage (ex ))
60- .isEqualTo ("Cause 1 -> Cause 2 -> IllegalAccessException" );
61- }
62-
63- @ Test
64- void testGetExceptionMessage_onlyTwo () {
65- var ex2 = new RuntimeException ("Cause 2" );
66- var ex = new RuntimeException ("Cause 1" , ex2 );
67- assertThat (ExceptionUtils .getExceptionMessage (ex )).isEqualTo ("Cause 1 -> Cause 2" );
68- }
69-
70- @ Test
71- void testGetExceptionMessage_moreThanThree () {
72- var ex4 = new RuntimeException ("Cause 4" );
73- var ex3 = new RuntimeException ("Cause 3" , ex4 );
74- var ex2 = new IllegalStateException (null , ex3 );
75- var ex = new RuntimeException ("Cause 1" , ex2 );
76- assertThat (ExceptionUtils .getExceptionMessage (ex ))
77- .isEqualTo ("Cause 1 -> IllegalStateException -> Cause 3" );
78- }
79-
8036 @ Test
8137 void testGetExceptionMessage_serializedThrowable () {
8238 var serializedException = new SerializedThrowable (new Exception ("Serialized Exception" ));
@@ -85,46 +41,25 @@ void testGetExceptionMessage_serializedThrowable() {
8541 }
8642
8743 @ Test
88- void testGetExceptionMessage_serializedThrowableWithDoubleSerializedException () {
89- var firstSerialized = new SerializedThrowable (new IndexOutOfBoundsException ("4>3" ));
90- var serializedException = new SerializedThrowable (firstSerialized );
91- assertThat (ExceptionUtils .getExceptionMessage (serializedException )).isEqualTo ("4>3" );
92- }
93-
94- @ Test
95- void testGetExceptionMessage_serializedThrowableWithRegularException () {
96- var serializedException = new SerializedThrowable (new Exception ("Serialized Exception" ));
97- var ex = new RuntimeException ("Cause 1" , serializedException );
98- assertThat (ExceptionUtils .getExceptionMessage (ex ))
99- .isEqualTo ("Cause 1 -> Serialized Exception" );
100- }
101-
102- @ Test
103- void testGetExceptionMessage_serializedThrowableAndAnotherCause () {
104- var ex = new RuntimeException ("Cause 1" );
105- var serializedException =
106- new SerializedThrowable (new RuntimeException ("Serialized Exception" , ex ));
107- assertThat (ExceptionUtils .getExceptionMessage (serializedException ))
108- .isEqualTo ("Serialized Exception -> Cause 1" );
109- }
110-
111- @ Test
112- void testGetExceptionMessage_moreThanThreeWithSerializedAnnRegularOnes () {
44+ void testGetExceptionMessage_differentKindsOfExceptions () {
11345 var ex4 = new RuntimeException ("Cause 4" );
11446 var ex3 = new RuntimeException ("Cause 3" , ex4 );
11547 var ex2 = new RuntimeException ("Cause 2" , new SerializedThrowable (ex3 ));
11648 var ex = new RuntimeException ("Cause 1" , ex2 );
11749 assertThat (ExceptionUtils .getExceptionMessage (ex ))
118- .isEqualTo ("Cause 1 -> Cause 2 -> Cause 3" );
50+ .isEqualTo ("Cause 1 -> Cause 2 -> Cause 3 -> Cause 4 " );
11951 }
12052
12153 @ Test
122- void testGetExceptionMessage_moreThanThreeWithSerializedAndNotWithDifferentOrdering () {
123- var ex4 = new RuntimeException ("Cause 4" );
124- var ex3 = new RuntimeException ("Cause 3" , ex4 );
125- var ex2 = new RuntimeException ("Cause 2" , new SerializedThrowable (ex3 ));
126- var ex = new RuntimeException ("Cause 1" , new SerializedThrowable (ex2 ));
127- assertThat (ExceptionUtils .getExceptionMessage (ex ))
128- .isEqualTo ("Cause 1 -> Cause 2 -> Cause 3" );
54+ void testSerializedThrowableError () {
55+ var serializedException = new SerializedThrowable (new NonSerializableException ());
56+ assertThat (ExceptionUtils .getExceptionMessage (serializedException ))
57+ .isEqualTo ("Unknown Error (SerializedThrowable)" );
58+ }
59+
60+ private static class NonSerializableException extends Exception {
61+ private void writeObject (java .io .ObjectOutputStream stream ) throws IOException {
62+ throw new IOException ();
63+ }
12964 }
13065}
0 commit comments