Skip to content

Commit

Permalink
fix: do not create new instances in nested tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgitario committed Mar 8, 2022
1 parent bf28520 commit b08a00e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
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

0 comments on commit b08a00e

Please sign in to comment.