Skip to content

Commit

Permalink
Unwrap AssertionError from InvocationTargetException during Quark…
Browse files Browse the repository at this point in the history
…us JUnit5 callbacks.

Previously, only `RuntimeException` derived throwables were unwrapped, but many test frameworks
use throwables deriving from `AssertionError`, so this should be handled also.
  • Loading branch information
msavy committed Aug 30, 2023
1 parent c352f20 commit fb42cc1
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ private void invokeCallbacks(List<Object> callbacks, String methodName, Class<?>
.invoke(callback, classInstance);
}
} catch (InvocationTargetException e) {
throw e.getCause() instanceof Exception ? (Exception) e.getCause() : e;
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause();
} else if (e.getCause() instanceof AssertionError) {
throw (AssertionError) e.getCause();
}
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.quarkus.test.junit;

import java.io.IOException;
import java.io.UncheckedIOException;

import org.junit.jupiter.api.AssertionFailureBuilder;

import io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback;
import io.quarkus.test.junit.callback.QuarkusTestMethodContext;

/**
* Test handling of {@link AssertionError}s and {@link RuntimeException} in callbacks.
*
* @see QuarkusTestCallbacksErrorHandlingTest
*/
public class ErrorThrowingCallback implements QuarkusTestAfterEachCallback {
@Override
public void afterEach(QuarkusTestMethodContext context) {
String throwableType = System.getProperty("quarkus.test.callback.throwableType");

if ("AssertionError".equalsIgnoreCase(throwableType)) {
AssertionFailureBuilder
.assertionFailure()
.expected("a")
.actual("b")
.reason("Oh no, it broke! Here's an assertion error")
.buildAndThrow();
}

if ("RuntimeException".equalsIgnoreCase(throwableType)) {
throw new UncheckedIOException(new IOException("Oh dear, it broke again"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.quarkus.test.junit;

import java.io.UncheckedIOException;
import java.util.List;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.callback.QuarkusTestMethodContext;

/**
* Generally, {@link AssertionError} is thrown by testing frameworks such as JUnit. However,
* it does not derive from {@link RuntimeException} hierarchy, and hence separate unwrapping of each type
* must be handled in {@link io.quarkus.test.junit.AbstractTestWithCallbacksExtension}.
* <p>
* Ensuring that <code>AssertionError</code> is at the top of any stack track allows tooling to properly
* parse and display expected vs actual diffs, etc.
*
* @see ErrorThrowingCallback
*/
public class QuarkusTestCallbacksErrorHandlingTest {

@Test
public void testAssertionErrorsAreUnwrappedFromCallback() {
Assertions.assertThrows(AssertionError.class, () -> {
System.setProperty("quarkus.test.callback.throwableType", "AssertionError");
MockCallbackExtension extension = new MockCallbackExtension();
QuarkusTestMethodContext mockContext = new QuarkusTestMethodContext(new Object(), List.of(), null, null);
extension.invokeAfterEachCallbacks(mockContext);
});
}

@Test
public void testRuntimeExceptionsAreUnwrappedFromCallback() {
Assertions.assertThrows(UncheckedIOException.class, () -> {
System.setProperty("quarkus.test.callback.throwableType", "RuntimeException");
MockCallbackExtension extension = new MockCallbackExtension();
QuarkusTestMethodContext mockContext = new QuarkusTestMethodContext(new Object(), List.of(), null, null);
extension.invokeAfterEachCallbacks(mockContext);
});
}

public static class MockCallbackExtension extends AbstractTestWithCallbacksExtension {

public MockCallbackExtension() throws ClassNotFoundException {
populateCallbacks(Thread.currentThread().getContextClassLoader());
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkus.test.junit.ErrorThrowingCallback

0 comments on commit fb42cc1

Please sign in to comment.