Skip to content

Commit afebb78

Browse files
committed
Streamline TestResult due to expectedExceptions
Closes #2788
1 parent bfdaf6d commit afebb78

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
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:2788: TestResult.isSuccess() is TRUE when test fails due to expectedExceptions (Krishnan Mahadevan)
23
Fixed: GITHUB-2800: Running Test Classes with Inherited @Factory and @DataProvider Annotated Non-Static Methods Fail (Krishnan Mahadevan)
34
New: Ability to provide custom error message for assertThrows\expectThrows methods (Anatolii Yuzhakov)
45
Fixed: GITHUB-2780: Use SpotBugs instead of abandoned FindBugs

testng-core/src/main/java/org/testng/internal/ExitCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class ExitCode {
1919
public static final int HAS_NO_TEST = 8;
2020
private static final int FAILED_WITHIN_SUCCESS = 4;
2121
public static final int SKIPPED = 2;
22-
private static final int FAILED = 1;
22+
public static final int FAILED = 1;
2323
private static final int SIZE = 3;
2424

2525
private final BitSet exitCodeBits;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,9 @@ private ITestResult invokeMethod(
726726
StatusHolder holder =
727727
considerExceptions(
728728
arguments.getTestMethod(), testResult, expectedExceptionClasses, failureContext);
729+
// After considering exceptions, the test status may have gotten updated.
730+
// So lets update our test status with the latest status obtained from StatusHolder
731+
testResult.setStatus(holder.status);
729732
runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);
730733
updateStatusHolderAccordingToTestResult(testResult, holder);
731734
boolean willRetryMethod =

testng-core/src/test/java/test/expectedexceptions/ExpectedExceptionsTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package test.expectedexceptions;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
35
import java.util.Collection;
46
import java.util.List;
57
import org.testng.Assert;
68
import org.testng.ITestResult;
9+
import org.testng.TestNG;
710
import org.testng.annotations.Test;
11+
import org.testng.internal.ExitCode;
812
import test.BaseTest;
9-
import test.expectedexceptions.github1409.TestClassSample;
13+
import test.expectedexceptions.issue2788.TestClassSample;
14+
import test.expectedexceptions.issue2788.TestClassSample.Local;
1015

1116
public class ExpectedExceptionsTest extends BaseTest {
1217

@@ -19,6 +24,15 @@ public void expectedExceptionsDeprecatedSyntax() {
1924
new String[] {});
2025
}
2126

27+
@Test(description = "GITHUB-2788")
28+
public void expectedExceptionsWithProperStatusPassedToListener() {
29+
TestNG testng = new TestNG();
30+
testng.setTestClasses(new Class[] {TestClassSample.class});
31+
testng.run();
32+
assertThat(testng.getStatus()).isEqualTo(ExitCode.FAILED);
33+
assertThat(Local.getInstance().isPass()).isFalse();
34+
}
35+
2236
@Test
2337
public void expectedExceptions() {
2438
runTest(
@@ -31,7 +45,7 @@ public void expectedExceptions() {
3145
@Test
3246
public void expectedExceptionsMessage() {
3347
getFailedTests().clear();
34-
addClass(TestClassSample.class);
48+
addClass(test.expectedexceptions.github1409.TestClassSample.class);
3549
run();
3650
Collection<List<ITestResult>> failedTests = getFailedTests().values();
3751
Assert.assertFalse(failedTests.isEmpty());
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package test.expectedexceptions.issue2788;
2+
3+
import org.testng.IInvokedMethod;
4+
import org.testng.IInvokedMethodListener;
5+
import org.testng.ITestResult;
6+
import org.testng.annotations.Listeners;
7+
import org.testng.annotations.Test;
8+
import test.expectedexceptions.issue2788.TestClassSample.Local;
9+
10+
@Listeners(Local.class)
11+
public class TestClassSample {
12+
13+
@Test(expectedExceptions = NullPointerException.class)
14+
public void sampleTestMethod() {}
15+
16+
public static class Local implements IInvokedMethodListener {
17+
18+
public static Local instance;
19+
private boolean pass;
20+
21+
private static void setInstance(Local localInstance) {
22+
instance = localInstance;
23+
}
24+
25+
public static Local getInstance() {
26+
return instance;
27+
}
28+
29+
public Local() {
30+
setInstance(this);
31+
}
32+
33+
public boolean isPass() {
34+
return pass;
35+
}
36+
37+
@Override
38+
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
39+
pass = testResult.isSuccess();
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)