diff --git a/bramble-api/src/test/java/org/briarproject/bramble/test/BrambleTestCase.java b/bramble-api/src/test/java/org/briarproject/bramble/test/BrambleTestCase.java index c2d90f37e..bac2196f3 100644 --- a/bramble-api/src/test/java/org/briarproject/bramble/test/BrambleTestCase.java +++ b/bramble-api/src/test/java/org/briarproject/bramble/test/BrambleTestCase.java @@ -7,34 +7,39 @@ import java.lang.Thread.UncaughtExceptionHandler; import java.util.logging.Level; import java.util.logging.Logger; +import javax.annotation.Nullable; + import static java.util.logging.Logger.getLogger; -import static org.junit.Assert.fail; public abstract class BrambleTestCase { private static final Logger LOG = getLogger(BrambleTestCase.class.getName()); - protected volatile boolean exceptionInBackgroundThread = false; + @Nullable + protected volatile Throwable exceptionInBackgroundThread = null; public BrambleTestCase() { // Ensure exceptions thrown on worker threads cause tests to fail UncaughtExceptionHandler fail = (thread, throwable) -> { LOG.log(Level.WARNING, "Caught unhandled exception", throwable); - exceptionInBackgroundThread = true; + exceptionInBackgroundThread = throwable; }; Thread.setDefaultUncaughtExceptionHandler(fail); } @Before public void beforeBrambleTestCase() { - exceptionInBackgroundThread = false; + exceptionInBackgroundThread = null; } @After public void afterBrambleTestCase() { - if (exceptionInBackgroundThread) { - fail("background thread has thrown an exception unexpectedly"); + Throwable thrown = exceptionInBackgroundThread; + if (thrown != null) { + throw new AssertionError( + "background thread has thrown an exception unexpectedly", + thrown); } } } diff --git a/bramble-api/src/test/java/org/briarproject/bramble/test/ThreadExceptionTest.java b/bramble-api/src/test/java/org/briarproject/bramble/test/ThreadExceptionTest.java index 464f9df0d..14a5d21bb 100644 --- a/bramble-api/src/test/java/org/briarproject/bramble/test/ThreadExceptionTest.java +++ b/bramble-api/src/test/java/org/briarproject/bramble/test/ThreadExceptionTest.java @@ -1,8 +1,8 @@ package org.briarproject.bramble.test; -import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; public class ThreadExceptionTest extends BrambleTestCase { @@ -29,8 +29,8 @@ public class ThreadExceptionTest extends BrambleTestCase { fail(); } - Assert.assertTrue(exceptionInBackgroundThread); - exceptionInBackgroundThread = false; + assertNotNull(exceptionInBackgroundThread); + exceptionInBackgroundThread = null; } }