From 28fc5cf5e6e2b3e0f3d3775869d27c4993b558b2 Mon Sep 17 00:00:00 2001 From: Jose Date: Mon, 11 Jul 2022 08:14:58 +0200 Subject: [PATCH] Clear outer instances when init test state is called The init test state is called several times depending whether JUnit 5 is configured with the lifecycle method based or class based. Clearing out the outer instances every time the main class is initialized, ensure that the outer instances are always up to date. --- .../it/main/QuarkusTestCallbacksTestCase.java | 4 +-- .../it/main/QuarkusTestNestedTestCase.java | 36 +++++++++++++++++-- .../TestContextCheckerAfterAllCallback.java | 18 ++++++++++ .../TestContextCheckerAfterEachCallback.java | 18 ++++++++++ ...TestContextCheckerBeforeEachCallback.java} | 10 ++++-- ...junit.callback.QuarkusTestAfterAllCallback | 1 + ...unit.callback.QuarkusTestAfterEachCallback | 1 + ...nit.callback.QuarkusTestBeforeEachCallback | 2 +- .../test/junit/QuarkusTestExtension.java | 1 + 9 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 integration-tests/main/src/test/java/io/quarkus/it/main/TestContextCheckerAfterAllCallback.java create mode 100644 integration-tests/main/src/test/java/io/quarkus/it/main/TestContextCheckerAfterEachCallback.java rename integration-tests/main/src/test/java/io/quarkus/it/main/{SimpleAnnotationCheckerBeforeEachCallback.java => TestContextCheckerBeforeEachCallback.java} (72%) create mode 100644 integration-tests/main/src/test/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterAllCallback create mode 100644 integration-tests/main/src/test/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback diff --git a/integration-tests/main/src/test/java/io/quarkus/it/main/QuarkusTestCallbacksTestCase.java b/integration-tests/main/src/test/java/io/quarkus/it/main/QuarkusTestCallbacksTestCase.java index 80983c0f53bc0..670422528a9e8 100644 --- a/integration-tests/main/src/test/java/io/quarkus/it/main/QuarkusTestCallbacksTestCase.java +++ b/integration-tests/main/src/test/java/io/quarkus/it/main/QuarkusTestCallbacksTestCase.java @@ -23,7 +23,7 @@ import io.quarkus.test.junit.QuarkusTest; /** - * The purpose of this test is simply to ensure that {@link SimpleAnnotationCheckerBeforeEachCallback} + * The purpose of this test is simply to ensure that {@link TestContextCheckerBeforeEachCallback} * can read {@code @TestAnnotation} without issue. * Also checks that {@link SimpleAnnotationCheckerBeforeClassCallback} is executed properly */ @@ -69,7 +69,7 @@ private void checkBeforeOrAfterEachTestInfo(TestInfo testInfo, String unexpected @TestAnnotation @Order(1) public void testTestMethodHasAnnotation() { - assertTrue(SimpleAnnotationCheckerBeforeEachCallback.testAnnotationChecked); + assertTrue(TestContextCheckerBeforeEachCallback.testAnnotationChecked); } @Test 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 57ab941d663c6..479600280ca33 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 @@ -59,6 +59,7 @@ void test() { assertEquals(0, COUNT_TEST.getAndIncrement(), "COUNT_TEST"); assertEquals(0, COUNT_AFTER_EACH.get(), "COUNT_AFTER_EACH"); assertEquals(0, COUNT_AFTER_ALL.get(), "COUNT_AFTER_ALL"); + assertEquals(0, TestContextCheckerBeforeEachCallback.OUTER_INSTANCES.size(), "Found unexpected outer instances"); } @Nested @@ -93,6 +94,18 @@ void testTwo() { assertEquals(0, COUNT_AFTER_ALL.get(), "COUNT_AFTER_ALL"); } + @Test + @Order(3) + void testOuterInstancesInBeforeEach() { + assertEquals(1, TestContextCheckerBeforeEachCallback.OUTER_INSTANCES.size()); + } + + @Test + @Order(4) + void testOuterInstancesInAfterEach() { + assertEquals(1, TestContextCheckerAfterEachCallback.OUTER_INSTANCES.size()); + } + @Test void testInnerAndOuterValues() { assertEquals(EXPECTED_INNER_VALUE, innerValue); @@ -122,7 +135,6 @@ void beforeEach() { @Test @Order(1) void testOne() { - // assertEquals(1, SECOND_LEVEL_COUNTER.get(), "SECOND_LEVEL_COUNTER"); assertEquals(1, SECOND_LEVEL_COUNTER.get(), "SECOND_LEVEL_COUNTER"); } @@ -134,6 +146,24 @@ void testSecondLevelAndInnerAndOuterValues() { assertEquals(EXPECTED_OUTER_VALUE, outerValue); assertEquals(EXPECTED_SECOND_LEVEL_FIRST_INNER_VALUE, secondLevelInnerValue); } + + @Test + @Order(3) + void testOuterInstancesInBeforeEach() { + assertEquals(2, TestContextCheckerBeforeEachCallback.OUTER_INSTANCES.size()); + } + + @Test + @Order(4) + void testOuterInstancesInAfterEach() { + assertEquals(2, TestContextCheckerAfterEachCallback.OUTER_INSTANCES.size()); + } + + @Test + @Order(5) + void testOuterInstancesInAfterAll() { + assertEquals(1, TestContextCheckerAfterAllCallback.OUTER_INSTANCES.size()); + } } } @@ -185,9 +215,9 @@ void afterEach() { @AfterAll static void afterAll() { assertEquals(1, COUNT_BEFORE_ALL.get(), "COUNT_BEFORE_ALL"); - assertEquals(15, COUNT_BEFORE_EACH.get(), "COUNT_BEFORE_EACH"); + assertEquals(25, COUNT_BEFORE_EACH.get(), "COUNT_BEFORE_EACH"); assertEquals(4, COUNT_TEST.get(), "COUNT_TEST"); - assertEquals(15, COUNT_AFTER_EACH.get(), "COUNT_AFTER_EACH"); + assertEquals(25, COUNT_AFTER_EACH.get(), "COUNT_AFTER_EACH"); assertEquals(0, COUNT_AFTER_ALL.getAndIncrement(), "COUNT_AFTER_ALL"); } } diff --git a/integration-tests/main/src/test/java/io/quarkus/it/main/TestContextCheckerAfterAllCallback.java b/integration-tests/main/src/test/java/io/quarkus/it/main/TestContextCheckerAfterAllCallback.java new file mode 100644 index 0000000000000..ba8af179ebfc2 --- /dev/null +++ b/integration-tests/main/src/test/java/io/quarkus/it/main/TestContextCheckerAfterAllCallback.java @@ -0,0 +1,18 @@ +package io.quarkus.it.main; + +import java.util.ArrayList; +import java.util.List; + +import io.quarkus.test.junit.callback.QuarkusTestAfterAllCallback; +import io.quarkus.test.junit.callback.QuarkusTestContext; + +public class TestContextCheckerAfterAllCallback implements QuarkusTestAfterAllCallback { + + public static final List OUTER_INSTANCES = new ArrayList<>(); + + @Override + public void afterAll(QuarkusTestContext context) { + OUTER_INSTANCES.clear(); + OUTER_INSTANCES.addAll(context.getOuterInstances()); + } +} diff --git a/integration-tests/main/src/test/java/io/quarkus/it/main/TestContextCheckerAfterEachCallback.java b/integration-tests/main/src/test/java/io/quarkus/it/main/TestContextCheckerAfterEachCallback.java new file mode 100644 index 0000000000000..b28d1511d49ff --- /dev/null +++ b/integration-tests/main/src/test/java/io/quarkus/it/main/TestContextCheckerAfterEachCallback.java @@ -0,0 +1,18 @@ +package io.quarkus.it.main; + +import java.util.ArrayList; +import java.util.List; + +import io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback; +import io.quarkus.test.junit.callback.QuarkusTestMethodContext; + +public class TestContextCheckerAfterEachCallback implements QuarkusTestAfterEachCallback { + + public static final List OUTER_INSTANCES = new ArrayList<>(); + + @Override + public void afterEach(QuarkusTestMethodContext context) { + OUTER_INSTANCES.clear(); + OUTER_INSTANCES.addAll(context.getOuterInstances()); + } +} diff --git a/integration-tests/main/src/test/java/io/quarkus/it/main/SimpleAnnotationCheckerBeforeEachCallback.java b/integration-tests/main/src/test/java/io/quarkus/it/main/TestContextCheckerBeforeEachCallback.java similarity index 72% rename from integration-tests/main/src/test/java/io/quarkus/it/main/SimpleAnnotationCheckerBeforeEachCallback.java rename to integration-tests/main/src/test/java/io/quarkus/it/main/TestContextCheckerBeforeEachCallback.java index 8e9447609046b..6ec5d0c7c8281 100644 --- a/integration-tests/main/src/test/java/io/quarkus/it/main/SimpleAnnotationCheckerBeforeEachCallback.java +++ b/integration-tests/main/src/test/java/io/quarkus/it/main/TestContextCheckerBeforeEachCallback.java @@ -1,17 +1,23 @@ package io.quarkus.it.main; import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; import io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback; import io.quarkus.test.junit.callback.QuarkusTestMethodContext; -public class SimpleAnnotationCheckerBeforeEachCallback implements QuarkusTestBeforeEachCallback { +public class TestContextCheckerBeforeEachCallback implements QuarkusTestBeforeEachCallback { + public static final List OUTER_INSTANCES = new ArrayList<>(); static boolean testAnnotationChecked; @Override public void beforeEach(QuarkusTestMethodContext context) { - // make sure that this comes into play only for the test we care about + OUTER_INSTANCES.clear(); + OUTER_INSTANCES.addAll(context.getOuterInstances()); + + // continue only if this comes into play only for the test we care about Method testMethod = context.getTestMethod(); if (!testMethod.getDeclaringClass().getName().endsWith("QuarkusTestCallbacksTestCase")) { diff --git a/integration-tests/main/src/test/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterAllCallback b/integration-tests/main/src/test/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterAllCallback new file mode 100644 index 0000000000000..04c5aa035d0ff --- /dev/null +++ b/integration-tests/main/src/test/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterAllCallback @@ -0,0 +1 @@ +io.quarkus.it.main.TestContextCheckerAfterAllCallback diff --git a/integration-tests/main/src/test/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback b/integration-tests/main/src/test/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback new file mode 100644 index 0000000000000..c9a184678b5c7 --- /dev/null +++ b/integration-tests/main/src/test/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback @@ -0,0 +1 @@ +io.quarkus.it.main.TestContextCheckerAfterEachCallback diff --git a/integration-tests/main/src/test/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback b/integration-tests/main/src/test/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback index 18e484433990e..d0f4906056035 100644 --- a/integration-tests/main/src/test/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback +++ b/integration-tests/main/src/test/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback @@ -1 +1 @@ -io.quarkus.it.main.SimpleAnnotationCheckerBeforeEachCallback +io.quarkus.it.main.TestContextCheckerBeforeEachCallback 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 62270cec1b3e9..ab85c6f7ce453 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 @@ -776,6 +776,7 @@ private void initTestState(ExtensionContext extensionContext, ExtensionState sta outerInstances.add(outerInstance); } } else { + outerInstances.clear(); actualTestInstance = runningQuarkusApplication.instance(actualTestClass); }