From b010110e1e5aedf528a125c7a406b30314ab7e4c Mon Sep 17 00:00:00 2001 From: Falko Modler Date: Tue, 31 Aug 2021 21:11:43 +0200 Subject: [PATCH] Fix NoSuchElementException on `@Before/AfterAll` with TestInfo Fixes #19800 (cherry picked from commit 6ac8ad3884bfee5915c42f14b0bb7bfff8d30baf) --- .../it/main/QuarkusTestCallbacksTestCase.java | 21 +++++++++++++++++++ .../test/junit/QuarkusTestExtension.java | 6 ++++-- 2 files changed, 25 insertions(+), 2 deletions(-) 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 af121205b21c2..80983c0f53bc0 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 @@ -3,6 +3,7 @@ import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -11,7 +12,9 @@ import java.lang.annotation.Target; import java.lang.reflect.Method; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; @@ -27,6 +30,22 @@ @QuarkusTest public class QuarkusTestCallbacksTestCase { + @BeforeAll + static void beforeAllWithTestInfo(TestInfo testInfo) { + checkBeforeOrAfterAllTestInfo(testInfo); + } + + @AfterAll + static void afterAllWithTestInfo(TestInfo testInfo) { + checkBeforeOrAfterAllTestInfo(testInfo); + } + + private static void checkBeforeOrAfterAllTestInfo(TestInfo testInfo) { + assertNotNull(testInfo); + assertEquals(QuarkusTestCallbacksTestCase.class, testInfo.getTestClass().get()); + assertFalse(testInfo.getTestMethod().isPresent()); + } + @BeforeEach void beforeEachWithTestInfo(TestInfo testInfo) throws NoSuchMethodException { checkBeforeOrAfterEachTestInfo(testInfo, "beforeEachWithTestInfo"); @@ -43,6 +62,7 @@ private void checkBeforeOrAfterEachTestInfo(TestInfo testInfo, String unexpected assertNotEquals(testMethodName, QuarkusTestCallbacksTestCase.class.getDeclaredMethod(unexpectedMethodName, TestInfo.class)); assertTrue(testMethodName.startsWith("test")); + assertEquals(QuarkusTestCallbacksTestCase.class, testInfo.getTestClass().get()); } @Test @@ -66,6 +86,7 @@ public void testInfoTestCase(TestInfo testInfo) throws NoSuchMethodException { Method testMethod = testInfo.getTestMethod().get(); assertEquals(testMethod, QuarkusTestCallbacksTestCase.class.getDeclaredMethod("testInfoTestCase", TestInfo.class)); assertEquals(1, testMethod.getAnnotationsByType(TestAnnotation.class).length); + assertEquals(QuarkusTestCallbacksTestCase.class, testInfo.getTestClass().get()); } @Target({ METHOD }) 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 a6175dce4e58e..5e91160ff6ff6 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 @@ -1038,9 +1038,11 @@ private Object runExtensionMethod(ReflectiveInvocationContext invocation cloneRequired = false; } else if (TestInfo.class.isAssignableFrom(theclass)) { TestInfo info = (TestInfo) arg; - Method newTestMethod = determineTCCLExtensionMethod(info.getTestMethod().get(), testClassFromTCCL); + Method newTestMethod = info.getTestMethod().isPresent() + ? determineTCCLExtensionMethod(info.getTestMethod().get(), testClassFromTCCL) + : null; replacement = new TestInfoImpl(info.getDisplayName(), info.getTags(), Optional.of(testClassFromTCCL), - Optional.of(newTestMethod)); + Optional.ofNullable(newTestMethod)); } else if (clonePattern.matcher(className).matches()) { cloneRequired = true; } else {