Skip to content

Commit

Permalink
Merge pull request #2815 from krmahadevan/fix_2788
Browse files Browse the repository at this point in the history
Streamline TestResult due to expectedExceptions
  • Loading branch information
juherr authored Nov 1, 2022
2 parents bfdaf6d + afebb78 commit 728f480
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Current
Fixed: GITHUB:2788: TestResult.isSuccess() is TRUE when test fails due to expectedExceptions (Krishnan Mahadevan)
Fixed: GITHUB-2800: Running Test Classes with Inherited @Factory and @DataProvider Annotated Non-Static Methods Fail (Krishnan Mahadevan)
New: Ability to provide custom error message for assertThrows\expectThrows methods (Anatolii Yuzhakov)
Fixed: GITHUB-2780: Use SpotBugs instead of abandoned FindBugs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ExitCode {
public static final int HAS_NO_TEST = 8;
private static final int FAILED_WITHIN_SUCCESS = 4;
public static final int SKIPPED = 2;
private static final int FAILED = 1;
public static final int FAILED = 1;
private static final int SIZE = 3;

private final BitSet exitCodeBits;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,9 @@ private ITestResult invokeMethod(
StatusHolder holder =
considerExceptions(
arguments.getTestMethod(), testResult, expectedExceptionClasses, failureContext);
// After considering exceptions, the test status may have gotten updated.
// So lets update our test status with the latest status obtained from StatusHolder
testResult.setStatus(holder.status);
runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);
updateStatusHolderAccordingToTestResult(testResult, holder);
boolean willRetryMethod =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package test.expectedexceptions;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Collection;
import java.util.List;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.TestNG;
import org.testng.annotations.Test;
import org.testng.internal.ExitCode;
import test.BaseTest;
import test.expectedexceptions.github1409.TestClassSample;
import test.expectedexceptions.issue2788.TestClassSample;
import test.expectedexceptions.issue2788.TestClassSample.Local;

public class ExpectedExceptionsTest extends BaseTest {

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

@Test(description = "GITHUB-2788")
public void expectedExceptionsWithProperStatusPassedToListener() {
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] {TestClassSample.class});
testng.run();
assertThat(testng.getStatus()).isEqualTo(ExitCode.FAILED);
assertThat(Local.getInstance().isPass()).isFalse();
}

@Test
public void expectedExceptions() {
runTest(
Expand All @@ -31,7 +45,7 @@ public void expectedExceptions() {
@Test
public void expectedExceptionsMessage() {
getFailedTests().clear();
addClass(TestClassSample.class);
addClass(test.expectedexceptions.github1409.TestClassSample.class);
run();
Collection<List<ITestResult>> failedTests = getFailedTests().values();
Assert.assertFalse(failedTests.isEmpty());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package test.expectedexceptions.issue2788;

import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import test.expectedexceptions.issue2788.TestClassSample.Local;

@Listeners(Local.class)
public class TestClassSample {

@Test(expectedExceptions = NullPointerException.class)
public void sampleTestMethod() {}

public static class Local implements IInvokedMethodListener {

public static Local instance;
private boolean pass;

private static void setInstance(Local localInstance) {
instance = localInstance;
}

public static Local getInstance() {
return instance;
}

public Local() {
setInstance(this);
}

public boolean isPass() {
return pass;
}

@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
pass = testResult.isSuccess();
}
}
}

0 comments on commit 728f480

Please sign in to comment.