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

Do not create new instances in nested tests #24172

Merged
merged 1 commit into from
Mar 9, 2022
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 @@ -33,6 +33,10 @@ public class QuarkusTestNestedTestCase {
private static final AtomicInteger COUNT_TEST = new AtomicInteger(0);
private static final AtomicInteger COUNT_AFTER_EACH = new AtomicInteger(0);
private static final AtomicInteger COUNT_AFTER_ALL = new AtomicInteger(0);
private static final String EXPECTED_OUTER_VALUE = "set from outer";
private static final String EXPECTED_INNER_VALUE = "set from inner";

String outerValue;

@BeforeAll
static void beforeAll() {
Expand All @@ -42,6 +46,7 @@ static void beforeAll() {
@BeforeEach
void beforeEach() {
COUNT_BEFORE_EACH.incrementAndGet();
outerValue = EXPECTED_OUTER_VALUE;
}

@Test
Expand All @@ -57,9 +62,12 @@ void test() {
@TestMethodOrder(OrderAnnotation.class)
class FirstNested {

String innerValue;

@BeforeEach
void beforeEach() {
COUNT_BEFORE_EACH.incrementAndGet();
innerValue = EXPECTED_INNER_VALUE;
}

@Test
Expand All @@ -82,6 +90,12 @@ void testTwo() {
assertEquals(0, COUNT_AFTER_ALL.get(), "COUNT_AFTER_ALL");
}

@Test
void testInnerAndOuterValues() {
assertEquals(EXPECTED_INNER_VALUE, innerValue);
assertEquals(EXPECTED_OUTER_VALUE, outerValue);
}

@AfterEach
void afterEach() {
COUNT_AFTER_EACH.incrementAndGet();
Expand Down Expand Up @@ -119,9 +133,9 @@ void afterEach() {
@AfterAll
static void afterAll() {
assertEquals(1, COUNT_BEFORE_ALL.get(), "COUNT_BEFORE_ALL");
assertEquals(7, COUNT_BEFORE_EACH.get(), "COUNT_BEFORE_EACH");
assertEquals(9, COUNT_BEFORE_EACH.get(), "COUNT_BEFORE_EACH");
assertEquals(4, COUNT_TEST.get(), "COUNT_TEST");
assertEquals(7, COUNT_AFTER_EACH.get(), "COUNT_AFTER_EACH");
assertEquals(9, COUNT_AFTER_EACH.get(), "COUNT_AFTER_EACH");
assertEquals(0, COUNT_AFTER_ALL.getAndIncrement(), "COUNT_AFTER_ALL");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -655,8 +655,7 @@ private void pushMockContext() {
Method pushContext = runningQuarkusApplication.getClassLoader().loadClass(MockSupport.class.getName())
.getDeclaredMethod("pushContext");
pushContext.setAccessible(true);
pushContext
.invoke(null);
pushContext.invoke(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -668,8 +667,7 @@ private void popMockContext() {
Method popContext = runningQuarkusApplication.getClassLoader().loadClass(MockSupport.class.getName())
.getDeclaredMethod("popContext");
popContext.setAccessible(true);
popContext
.invoke(null);
popContext.invoke(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -907,15 +905,16 @@ private Object runExtensionMethod(ReflectiveInvocationContext<Method> invocation
try {
Class<?> testClassFromTCCL = Class.forName(extensionContext.getRequiredTestClass().getName(), true,
Thread.currentThread().getContextClassLoader());
Object effectiveTestInstance = actualTestInstance;
Method newMethod = determineTCCLExtensionMethod(invocationContext.getExecutable(), 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.getExecutable(), testClassFromTCCL);
methodFromEnclosing = true;
newMethod = determineTCCLExtensionMethod(invocationContext.getExecutable(),
testClassFromTCCL.getEnclosingClass());
effectiveTestInstance = outerInstance;
}

if (newMethod == null) {
throw new RuntimeException("Could not find method " + invocationContext.getExecutable() + " on test class");
}
Expand Down Expand Up @@ -988,12 +987,6 @@ private Object runExtensionMethod(ReflectiveInvocationContext<Method> invocation
}
}

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();
}
if (testMethodInvokerToUse != null) {
return testMethodInvokerToUse.getClass()
.getMethod("invoke", Object.class, Method.class, List.class, String.class)
Expand Down