Skip to content

Commit 1d9dba8

Browse files
committed
Ensure ITestResult injected to @AfterMethod is apt
Closes #3006
1 parent 94f3373 commit 1d9dba8

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Current
2+
Fixed: GITHUB-3006: ITestResult injected at @AfterMethod incorrect when a configuration method failed (Krishnan Mahadevan)
23
Fixed: GITHUB-3003: BeforeClass|AfterClass with inheritedGroups triggers cyclic dependencies (Krishnan Mahadevan)
34
New: Added @Inherited to the Listeners annotation, allowing it to be used in forming meta-annotations. (Pavlo Glushchenko)
45
Fixed: GITHUB-2991: Suite attributes map should be thread safe (Krishnan Mahadevan)

testng-core/src/main/java/org/testng/internal/invokers/TestInvoker.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,10 +595,9 @@ private ITestResult invokeMethod(
595595
TestResult.copyAttributes(testResult, result);
596596
m_notifier.addSkippedTest(arguments.getTestMethod(), result);
597597
arguments.getTestMethod().incrementCurrentInvocationCount();
598-
testResult.setMethod(arguments.getTestMethod());
599598
invokedMethod = new InvokedMethod(startTime, result);
600599
invokeListenersForSkippedTestResult(result, invokedMethod);
601-
runAfterConfigurations(arguments, suite, testResult);
600+
runAfterConfigurations(arguments, suite, result);
602601
runAfterGroupsConfigurations(arguments);
603602

604603
return result;
@@ -773,7 +772,7 @@ private static void cleanInterruptStatus() {
773772
}
774773

775774
private void runAfterConfigurations(
776-
TestMethodArguments arguments, XmlSuite suite, TestResult testResult) {
775+
TestMethodArguments arguments, XmlSuite suite, ITestResult testResult) {
777776
ITestNGMethod[] teardownConfigMethods =
778777
TestNgMethodUtils.filterTeardownConfigurationMethods(
779778
arguments.getTestMethod(), arguments.getAfterMethods());
@@ -795,7 +794,7 @@ private void runAfterGroupsConfigurations(TestMethodArguments arguments) {
795794
private void runConfigMethods(
796795
TestMethodArguments arguments,
797796
XmlSuite suite,
798-
TestResult testResult,
797+
ITestResult testResult,
799798
ITestNGMethod[] teardownConfigMethods) {
800799
ConfigMethodArguments cfgArgs =
801800
new ConfigMethodArguments.Builder()

testng-core/src/test/java/test/configuration/ConfigurationTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
import java.util.Arrays;
66
import java.util.List;
7+
import java.util.stream.Collectors;
78
import org.testng.Assert;
9+
import org.testng.ITestNGMethod;
10+
import org.testng.ITestResult;
811
import org.testng.TestNG;
912
import org.testng.annotations.DataProvider;
1013
import org.testng.annotations.Test;
@@ -76,6 +79,22 @@ public void ensureGroupInheritanceWorksForConfigMethods() {
7679
assertThat(test.configuration.issue3003.TestClassSample.logs).containsAll(expected);
7780
}
7881

82+
@Test(description = "GITHUB-3006")
83+
public void ensureNativelyInjectedTestResultForAfterMethodMatchesTestMethod() {
84+
TestNG testng = create(test.configuration.issue3006.TestClassSample.class);
85+
testng.run();
86+
ITestResult actual = test.configuration.issue3006.TestClassSample.iTestResult;
87+
assertThat(actual.getStatus())
88+
.withFailMessage("The test method status should have been SKIPPED")
89+
.isEqualTo(ITestResult.SKIP);
90+
List<String> skippedDueTo =
91+
actual.getSkipCausedBy().stream()
92+
.map(ITestNGMethod::getQualifiedName)
93+
.collect(Collectors.toList());
94+
assertThat(skippedDueTo)
95+
.containsExactly("test.configuration.issue3006.TestClassSample.beforeMethod");
96+
}
97+
7998
@DataProvider(name = "produceTestClasses")
8099
public Object[][] produceTestClasses() {
81100
return new Object[][] {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package test.configuration.issue3006;
2+
3+
import org.testng.ITestResult;
4+
import org.testng.annotations.AfterMethod;
5+
import org.testng.annotations.BeforeMethod;
6+
import org.testng.annotations.Test;
7+
8+
public class TestClassSample {
9+
10+
public static ITestResult iTestResult;
11+
12+
@BeforeMethod
13+
public void beforeMethod() {
14+
throw new RuntimeException("Exception to simulate configuration error");
15+
}
16+
17+
@Test
18+
public void test() {}
19+
20+
@AfterMethod(alwaysRun = true)
21+
public void afterMethod(ITestResult testResult) {
22+
iTestResult = testResult;
23+
}
24+
}

0 commit comments

Comments
 (0)