-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unwrap
AssertionError
from InvocationTargetException
during Quark…
…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
Showing
4 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
test-framework/junit5/src/test/java/io/quarkus/test/junit/ErrorThrowingCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ork/junit5/src/test/java/io/quarkus/test/junit/QuarkusTestCallbacksErrorHandlingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...t/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.quarkus.test.junit.ErrorThrowingCallback |