Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unwrap InvocationTargetException thrown by QuarkusTest*Callback #23989

Merged
merged 1 commit into from
Feb 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<Class<?>, ?> tuple = createQuarkusTestMethodContextTuple(context);
afterEachCallback.getClass().getMethod("afterEach", tuple.getKey())
.invoke(afterEachCallback, tuple.getValue());
}
try {
for (Object afterEachCallback : afterEachCallbacks) {
Map.Entry<Class<?>, ?> tuple = createQuarkusTestMethodContextTuple(context);
afterEachCallback.getClass().getMethod("afterEach", tuple.getKey())
.invoke(afterEachCallback, tuple.getValue());
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running this outside of the TCCL restoration block looked wrong, so I moved it inside.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... Not sure about this... Did you see an issue with the existing code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not an actual one, but compare it with this:
https://github.com/quarkusio/quarkus/blob/2.7.2.Final/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java#L407

Why should the TCCL only be restored if there is an exception in a beforeEachCallback, but not if there is an exception in an afterEachCallback? Doesn't make sense to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS: afterAll also restores the TCCL just like beforeEach. Same happens for beforeClassCallback. 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks right to me

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);
}
Expand Down Expand Up @@ -711,6 +715,8 @@ public <T> T interceptTestClassConstructor(Invocation<T> 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);
}
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
Expand Down