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

Ensure @QuarkusTest works with @Nested and test lifecycle methods #17997

Merged
merged 1 commit into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
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 @@ -2,6 +2,10 @@

import static org.hamcrest.Matchers.contains;

import java.util.concurrent.atomic.AtomicInteger;

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

Expand All @@ -11,12 +15,26 @@
@QuarkusTest
public class JaxbTestCase {

private static final AtomicInteger count = new AtomicInteger(0);

@BeforeEach
public void beforeInEnclosing() {
count.incrementAndGet();
}

@Nested
class SomeClass {

@BeforeEach
public void beforeInTest() {
count.incrementAndGet();
}

@Test
public void testNews() {
RestAssured.when().get("/test/jaxb/getnews").then()
.body("author", contains("Emmanuel Bernard"));
Assertions.assertEquals(2, count.get());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -919,34 +919,19 @@ public void interceptAfterAllMethod(Invocation<Void> invocation, ReflectiveInvoc
private Object runExtensionMethod(ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext)
throws Throwable {
resetHangTimeout();
Method newMethod = null;

ClassLoader old = setCCL(runningQuarkusApplication.getClassLoader());
try {
Class<?> c = Class.forName(extensionContext.getRequiredTestClass().getName(), true,
Class<?> testClassFromTCCL = Class.forName(extensionContext.getRequiredTestClass().getName(), true,
Thread.currentThread().getContextClassLoader());
while (c != Object.class) {
if (c.getName().equals(invocationContext.getExecutable().getDeclaringClass().getName())) {
try {
Class<?>[] originalParameterTypes = invocationContext.getExecutable().getParameterTypes();
List<Class<?>> parameterTypesFromTccl = new ArrayList<>(originalParameterTypes.length);
for (Class<?> type : originalParameterTypes) {
if (type.isPrimitive()) {
parameterTypesFromTccl.add(type);
} else {
parameterTypesFromTccl
.add(Class.forName(type.getName(), true,
Thread.currentThread().getContextClassLoader()));
}
}
newMethod = c.getDeclaredMethod(invocationContext.getExecutable().getName(),
parameterTypesFromTccl.toArray(new Class[0]));
break;
} catch (NoSuchMethodException ignored) {

}
}
c = c.getSuperclass();
Method newMethod = determineTCCLExtensionMethod(invocationContext, testClassFromTCCL);
boolean methodFromEnclosing = false;
// this is needed to support before*** and after*** methods that are part of class that encloses the test class
// (the test class is in this case a @Nested test)
if ((newMethod == null) && (testClassFromTCCL.getEnclosingClass() != null)) {
testClassFromTCCL = testClassFromTCCL.getEnclosingClass();
newMethod = determineTCCLExtensionMethod(invocationContext, testClassFromTCCL);
methodFromEnclosing = true;
}
if (newMethod == null) {
throw new RuntimeException("Could not find method " + invocationContext.getExecutable() + " on test class");
Expand Down Expand Up @@ -990,7 +975,13 @@ private Object runExtensionMethod(ReflectiveInvocationContext<Method> invocation
}
}

return newMethod.invoke(actualTestInstance, argumentsFromTccl.toArray(new Object[0]));
Object effectiveTestInstance = actualTestInstance;
if (methodFromEnclosing) {
// TODO: this is a little dodgy, ideally we would need to use the same constructor that was used for the original object
// but it's unlikely(?) we will run into this combo
effectiveTestInstance = testClassFromTCCL.getConstructor().newInstance();
}
return newMethod.invoke(effectiveTestInstance, argumentsFromTccl.toArray(new Object[0]));
} catch (InvocationTargetException e) {
throw e.getCause();
} catch (IllegalAccessException | ClassNotFoundException e) {
Expand All @@ -1000,6 +991,35 @@ private Object runExtensionMethod(ReflectiveInvocationContext<Method> invocation
}
}

private Method determineTCCLExtensionMethod(ReflectiveInvocationContext<Method> invocationContext, Class<?> c)
throws ClassNotFoundException {
Method newMethod = null;
while (c != Object.class) {
if (c.getName().equals(invocationContext.getExecutable().getDeclaringClass().getName())) {
try {
Class<?>[] originalParameterTypes = invocationContext.getExecutable().getParameterTypes();
List<Class<?>> parameterTypesFromTccl = new ArrayList<>(originalParameterTypes.length);
for (Class<?> type : originalParameterTypes) {
if (type.isPrimitive()) {
parameterTypesFromTccl.add(type);
} else {
parameterTypesFromTccl
.add(Class.forName(type.getName(), true,
Thread.currentThread().getContextClassLoader()));
}
}
newMethod = c.getDeclaredMethod(invocationContext.getExecutable().getName(),
parameterTypesFromTccl.toArray(new Class[0]));
break;
} catch (NoSuchMethodException ignored) {

}
}
c = c.getSuperclass();
}
return newMethod;
}

@Override
public void afterAll(ExtensionContext context) throws Exception {
resetHangTimeout();
Expand Down