diff --git a/integration-tests/main/src/test/java/io/quarkus/it/main/QuarkusTestNestedTestCase.java b/integration-tests/main/src/test/java/io/quarkus/it/main/QuarkusTestNestedTestCase.java index 30bb5e2fc1430e..fffde15d1638f4 100644 --- a/integration-tests/main/src/test/java/io/quarkus/it/main/QuarkusTestNestedTestCase.java +++ b/integration-tests/main/src/test/java/io/quarkus/it/main/QuarkusTestNestedTestCase.java @@ -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"); } } diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java index 21f57216d9732b..ce8a4ba43924bc 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java @@ -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,16 @@ private Object runExtensionMethod(ReflectiveInvocationContext 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"); } @@ -988,12 +987,6 @@ private Object runExtensionMethod(ReflectiveInvocationContext 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)