Skip to content

Commit

Permalink
Check for StackOverflowError as root cause
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcono1234 committed Feb 22, 2025
1 parent 9a69c64 commit 88e389c
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.common.base.Throwables;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
Expand Down Expand Up @@ -102,10 +103,10 @@ public JsonElement serialize(

/** Asserts that a {@link StackOverflowError} is thrown. */
private static void assertThrowsStackOverflow(ThrowingRunnable runnable) {
// In most cases a StackOverflowError is thrown; however if that error occurs within the JDK
// code, it might actually be wrapped in another exception class, for example InternalError
// Because this is JDK implementation dependent assume at least that any Throwable is thrown
assertThrows(Throwable.class, runnable);
// Obtain the root cause because the StackOverflowError might occur in JDK code, and that might
// wrap it in another exception class, for example InternalError
Throwable t = assertThrows(Throwable.class, runnable);
assertThat(Throwables.getRootCause(t)).isInstanceOf(StackOverflowError.class);
}

@Test
Expand Down

0 comments on commit 88e389c

Please sign in to comment.