Consider following test code:
@Test
public void test() throws InterruptedException {
var latch = new CountDownLatch(1);
Executors.newSingleThreadExecutor().execute(() -> {
latch.countDown();
Assert.fail("Doesn't fail the test");
});
latch.await();
}
It prints exception, but passes.
Exception in thread "pool-1-thread-1" java.lang.AssertionError: Doesn't fail the test at org.junit.Assert.fail(Assert.java:89) at MyTest.lambda$test$0(MyTest.java:55) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:834)
I have tried to set custom uncaught exception handler setUncaughtExceptionHandler((t, e) -> Assert.fail(e.getMessage()))
, but this didn't help.