From 83c3b4b9544fbe2be635c2b5aee5a61b1b2d68a3 Mon Sep 17 00:00:00 2001 From: Falko Modler Date: Sat, 26 Feb 2022 00:27:49 +0100 Subject: [PATCH] Unwrap InvocationTargetException thrown by QuarkusTest*Callback --- .../test/junit/QuarkusTestExtension.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java index 047b6007f2ff1..21f57216d9732 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java @@ -421,6 +421,8 @@ public void beforeEach(ExtensionContext context) throws Exception { runningQuarkusApplication.getClassLoader().loadClass(TestScopeManager.class.getName()) .getDeclaredMethod("setup", boolean.class).invoke(null, false); } + } catch (InvocationTargetException e) { + throw e.getCause() instanceof Exception ? (Exception) e.getCause() : e; } finally { setCCL(original); } @@ -504,16 +506,18 @@ public void afterEach(ExtensionContext context) throws Exception { if (!failedBoot) { popMockContext(); ClassLoader original = setCCL(runningQuarkusApplication.getClassLoader()); - for (Object afterEachCallback : afterEachCallbacks) { - Map.Entry, ?> tuple = createQuarkusTestMethodContextTuple(context); - afterEachCallback.getClass().getMethod("afterEach", tuple.getKey()) - .invoke(afterEachCallback, tuple.getValue()); - } try { + for (Object afterEachCallback : afterEachCallbacks) { + Map.Entry, ?> tuple = createQuarkusTestMethodContextTuple(context); + afterEachCallback.getClass().getMethod("afterEach", tuple.getKey()) + .invoke(afterEachCallback, tuple.getValue()); + } runningQuarkusApplication.getClassLoader().loadClass(RestAssuredURLManager.class.getName()) .getDeclaredMethod("clearURL").invoke(null); runningQuarkusApplication.getClassLoader().loadClass(TestScopeManager.class.getName()) .getDeclaredMethod("tearDown", boolean.class).invoke(null, false); + } catch (InvocationTargetException e) { + throw e.getCause() instanceof Exception ? (Exception) e.getCause() : e; } finally { setCCL(original); } @@ -711,6 +715,8 @@ public T interceptTestClassConstructor(Invocation invocation, beforeClassCallback.getClass().getMethod("beforeClass", Class.class).invoke(beforeClassCallback, runningQuarkusApplication.getClassLoader().loadClass(requiredTestClass.getName())); } + } catch (InvocationTargetException e) { + throw e.getCause(); } finally { Thread.currentThread().setContextClassLoader(old); } @@ -783,7 +789,8 @@ private void initTestState(ExtensionContext extensionContext, ExtensionState sta } } } catch (Exception e) { - throw new TestInstantiationException("Failed to create test instance", e); + throw new TestInstantiationException("Failed to create test instance", + e instanceof InvocationTargetException ? e.getCause() : e); } } @@ -1089,6 +1096,8 @@ private void runAfterAllCallbacks(ExtensionContext context) throws Exception { afterAllCallback.getClass().getMethod("afterAll", quarkusTestContextClass) .invoke(afterAllCallback, quarkusTestContextInstance); } + } catch (InvocationTargetException e) { + throw e.getCause() instanceof Exception ? (Exception) e.getCause() : e; } finally { setCCL(original); }