Skip to content

Commit

Permalink
Fix NoSuchElementException on @Before/AfterAll with TestInfo
Browse files Browse the repository at this point in the history
Fixes quarkusio#19800

(cherry picked from commit 6ac8ad3)
  • Loading branch information
famod authored and gsmet committed Sep 6, 2021
1 parent 6e88f4e commit b010110
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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");
Expand All @@ -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
Expand All @@ -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 })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1038,9 +1038,11 @@ private Object runExtensionMethod(ReflectiveInvocationContext<Method> 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 {
Expand Down

0 comments on commit b010110

Please sign in to comment.