Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not create new instances in nested tests
Browse files Browse the repository at this point in the history
Sgitario committed Mar 8, 2022
1 parent bf28520 commit 62555a7
Showing 2 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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() {
@@ -42,6 +46,7 @@ static void beforeAll() {
@BeforeEach
void beforeEach() {
COUNT_BEFORE_EACH.incrementAndGet();
outerValue = EXPECTED_OUTER_VALUE;
}

@Test
@@ -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
@@ -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();
@@ -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
@@ -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);
}
@@ -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);
}
@@ -907,15 +905,17 @@ 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;
Class<?> enclosingClass = testClassFromTCCL.getEnclosingClass();
while (newMethod == null && enclosingClass != null) {
newMethod = determineTCCLExtensionMethod(invocationContext.getExecutable(), enclosingClass);
effectiveTestInstance = outerInstance;
enclosingClass = enclosingClass.getEnclosingClass();
}

if (newMethod == null) {
throw new RuntimeException("Could not find method " + invocationContext.getExecutable() + " on test class");
}
@@ -988,12 +988,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)

0 comments on commit 62555a7

Please sign in to comment.