diff --git a/integration-tests/main/src/test/java/io/quarkus/it/main/QuarkusTestNestedPerClassLifecycleTestCase.java b/integration-tests/main/src/test/java/io/quarkus/it/main/QuarkusTestNestedPerClassLifecycleTestCase.java index 19fc04c59aeb22..06085de5f96c7c 100644 --- a/integration-tests/main/src/test/java/io/quarkus/it/main/QuarkusTestNestedPerClassLifecycleTestCase.java +++ b/integration-tests/main/src/test/java/io/quarkus/it/main/QuarkusTestNestedPerClassLifecycleTestCase.java @@ -4,6 +4,7 @@ import java.util.concurrent.atomic.AtomicInteger; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Tag; @@ -27,7 +28,16 @@ public class QuarkusTestNestedPerClassLifecycleTestCase { private final AtomicInteger counter = new AtomicInteger(0); @BeforeAll - public void increment() { + public void incrementInBeforeAll() { + counter.incrementAndGet(); + } + + /** + * We're doing nothing with this code, but we want to keep it to verify the methods annotated + * with `@AfterAll` work for nested tests. + */ + @AfterAll + public void incrementInAfterAll() { counter.incrementAndGet(); } 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 b9ef9bfbbd0143..be03f716f1e745 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 @@ -15,8 +15,10 @@ import java.time.Duration; import java.time.temporal.ChronoUnit; import java.util.AbstractMap; +import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections; +import java.util.Deque; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -121,7 +123,7 @@ public class QuarkusTestExtension extends AbstractJvmQuarkusTestExtension private static Class actualTestClass; private static Object actualTestInstance; // needed for @Nested - private static List outerInstances = new ArrayList<>(1); + private static Deque outerInstances = new ArrayDeque<>(1); private static RunningQuarkusApplication runningQuarkusApplication; private static Pattern clonePattern; private static Throwable firstException; //if this is set then it will be thrown from the very first test that is run, the rest are aborted @@ -561,7 +563,7 @@ public void afterEach(ExtensionContext context) throws Exception { Constructor constructor = quarkusTestMethodContextClass.getConstructor(Object.class, List.class, Method.class); return new AbstractMap.SimpleEntry<>(quarkusTestMethodContextClass, - constructor.newInstance(actualTestInstance, outerInstances, actualTestMethod)); + constructor.newInstance(actualTestInstance, new ArrayList<>(outerInstances), actualTestMethod)); } private boolean isNativeOrIntegrationTest(Class clazz) { @@ -1078,7 +1080,9 @@ public void afterAll(ExtensionContext context) throws Exception { } } finally { currentTestClassStack.pop(); - outerInstances.clear(); + if (!outerInstances.isEmpty()) { + actualTestInstance = outerInstances.pop(); + } } }