From c7028f858aab1fd9df53c93a66bd6b8b32448c2a Mon Sep 17 00:00:00 2001 From: Krishnan Mahadevan Date: Tue, 21 Nov 2023 10:41:40 +0530 Subject: [PATCH] Ensure ITestResult injected to @AfterMethod is apt Closes #3006 --- CHANGES.txt | 1 + .../testng/internal/invokers/TestInvoker.java | 7 +++--- .../test/configuration/ConfigurationTest.java | 19 +++++++++++++++ .../issue3006/TestClassSample.java | 24 +++++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 testng-core/src/test/java/test/configuration/issue3006/TestClassSample.java diff --git a/CHANGES.txt b/CHANGES.txt index 068e3c082..477d9411d 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ Current +Fixed: GITHUB-3006: ITestResult injected at @AfterMethod incorrect when a configuration method failed (Krishnan Mahadevan) Fixed: GITHUB-3003: BeforeClass|AfterClass with inheritedGroups triggers cyclic dependencies (Krishnan Mahadevan) New: Added @Inherited to the Listeners annotation, allowing it to be used in forming meta-annotations. (Pavlo Glushchenko) Fixed: GITHUB-2991: Suite attributes map should be thread safe (Krishnan Mahadevan) diff --git a/testng-core/src/main/java/org/testng/internal/invokers/TestInvoker.java b/testng-core/src/main/java/org/testng/internal/invokers/TestInvoker.java index a1a468561..d9f02cee9 100644 --- a/testng-core/src/main/java/org/testng/internal/invokers/TestInvoker.java +++ b/testng-core/src/main/java/org/testng/internal/invokers/TestInvoker.java @@ -595,10 +595,9 @@ private ITestResult invokeMethod( TestResult.copyAttributes(testResult, result); m_notifier.addSkippedTest(arguments.getTestMethod(), result); arguments.getTestMethod().incrementCurrentInvocationCount(); - testResult.setMethod(arguments.getTestMethod()); invokedMethod = new InvokedMethod(startTime, result); invokeListenersForSkippedTestResult(result, invokedMethod); - runAfterConfigurations(arguments, suite, testResult); + runAfterConfigurations(arguments, suite, result); runAfterGroupsConfigurations(arguments); return result; @@ -773,7 +772,7 @@ private static void cleanInterruptStatus() { } private void runAfterConfigurations( - TestMethodArguments arguments, XmlSuite suite, TestResult testResult) { + TestMethodArguments arguments, XmlSuite suite, ITestResult testResult) { ITestNGMethod[] teardownConfigMethods = TestNgMethodUtils.filterTeardownConfigurationMethods( arguments.getTestMethod(), arguments.getAfterMethods()); @@ -795,7 +794,7 @@ private void runAfterGroupsConfigurations(TestMethodArguments arguments) { private void runConfigMethods( TestMethodArguments arguments, XmlSuite suite, - TestResult testResult, + ITestResult testResult, ITestNGMethod[] teardownConfigMethods) { ConfigMethodArguments cfgArgs = new ConfigMethodArguments.Builder() diff --git a/testng-core/src/test/java/test/configuration/ConfigurationTest.java b/testng-core/src/test/java/test/configuration/ConfigurationTest.java index c8d1e3d72..f88b3e27b 100644 --- a/testng-core/src/test/java/test/configuration/ConfigurationTest.java +++ b/testng-core/src/test/java/test/configuration/ConfigurationTest.java @@ -4,7 +4,10 @@ import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import org.testng.Assert; +import org.testng.ITestNGMethod; +import org.testng.ITestResult; import org.testng.TestNG; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @@ -76,6 +79,22 @@ public void ensureGroupInheritanceWorksForConfigMethods() { assertThat(test.configuration.issue3003.TestClassSample.logs).containsAll(expected); } + @Test(description = "GITHUB-3006") + public void ensureNativelyInjectedTestResultForAfterMethodMatchesTestMethod() { + TestNG testng = create(test.configuration.issue3006.TestClassSample.class); + testng.run(); + ITestResult actual = test.configuration.issue3006.TestClassSample.iTestResult; + assertThat(actual.getStatus()) + .withFailMessage("The test method status should have been SKIPPED") + .isEqualTo(ITestResult.SKIP); + List skippedDueTo = + actual.getSkipCausedBy().stream() + .map(ITestNGMethod::getQualifiedName) + .collect(Collectors.toList()); + assertThat(skippedDueTo) + .containsExactly("test.configuration.issue3006.TestClassSample.beforeMethod"); + } + @DataProvider(name = "produceTestClasses") public Object[][] produceTestClasses() { return new Object[][] { diff --git a/testng-core/src/test/java/test/configuration/issue3006/TestClassSample.java b/testng-core/src/test/java/test/configuration/issue3006/TestClassSample.java new file mode 100644 index 000000000..51ed573a2 --- /dev/null +++ b/testng-core/src/test/java/test/configuration/issue3006/TestClassSample.java @@ -0,0 +1,24 @@ +package test.configuration.issue3006; + +import org.testng.ITestResult; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class TestClassSample { + + public static ITestResult iTestResult; + + @BeforeMethod + public void beforeMethod() { + throw new RuntimeException("Exception to simulate configuration error"); + } + + @Test + public void test() {} + + @AfterMethod(alwaysRun = true) + public void afterMethod(ITestResult testResult) { + iTestResult = testResult; + } +}