Skip to content

Commit c377d9d

Browse files
committed
Handle exceptions in emailable Reporter
Closes #3038
1 parent 93223f6 commit c377d9d

File tree

7 files changed

+104
-3
lines changed

7 files changed

+104
-3
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Current
22
7.10.0
3+
Fixed: GITHUB-3038: java.lang.IllegalStateException: Results per method should NOT have been empty (Krishnan Mahadevan)
34
Fixed: GITHUB-3022: Remove deprecated JUnit related support in TestNG (Krishnan Mahadevan)
45

56
7.9.0

testng-core/src/main/java/org/testng/reporters/EmailableReporter2.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ protected List<ClassResult> groupResults(Set<ITestResult> results) {
750750
String className = result.getTestClass().getName();
751751
if (!previousClassName.equals(className)) {
752752
// Different class implies different method
753-
if (!resultsPerMethod.isEmpty()) {
753+
if (resultsPerMethod.isEmpty()) {
754754
throw new IllegalStateException("Results per method should NOT have been empty");
755755
}
756756
resultsPerClass.add(new MethodResult(resultsPerMethod));
@@ -764,7 +764,9 @@ protected List<ClassResult> groupResults(Set<ITestResult> results) {
764764
} else {
765765
String methodName = result.getMethod().getMethodName();
766766
if (!previousMethodName.equals(methodName)) {
767-
assert !resultsPerMethod.isEmpty();
767+
if (resultsPerMethod.isEmpty()) {
768+
throw new IllegalStateException("Results per method should NOT have been empty");
769+
}
768770
resultsPerClass.add(new MethodResult(resultsPerMethod));
769771
resultsPerMethod = Lists.newArrayList();
770772

@@ -773,7 +775,7 @@ protected List<ClassResult> groupResults(Set<ITestResult> results) {
773775
}
774776
resultsPerMethod.add(result);
775777
}
776-
if (!resultsPerMethod.isEmpty()) {
778+
if (resultsPerMethod.isEmpty()) {
777779
throw new IllegalStateException("Results per method should NOT have been empty");
778780
}
779781
resultsPerClass.add(new MethodResult(resultsPerMethod));

testng-core/src/test/java/test/reports/EmailableReporterTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
import org.testng.annotations.Test;
1212
import org.testng.reporters.EmailableReporter2;
1313
import test.SimpleBaseTest;
14+
import test.reports.issue3038.AnotherTestCaseSample;
15+
import test.reports.issue3038.ExceptionAwareEmailableReporter;
16+
import test.reports.issue3038.TestCaseSample;
17+
import test.reports.issue3038.TestCaseWithConfigProblemSample;
1418

1519
public class EmailableReporterTest extends SimpleBaseTest {
1620
@Test(dataProvider = "getReporterInstances", priority = 1)
@@ -35,6 +39,37 @@ public void testReportsNameCustomizationViaMainMethodInvocationAndJVMArguments(
3539
runTestViaMainMethod(clazzName, jvm);
3640
}
3741

42+
@Test
43+
public void ensureEmailableReportsDontThrowExceptions() {
44+
TestNG testng = create(TestCaseSample.class);
45+
ExceptionAwareEmailableReporter reporter = new ExceptionAwareEmailableReporter();
46+
testng.addListener(reporter);
47+
testng.run();
48+
assertThat(reporter.hasError).isFalse();
49+
}
50+
51+
@Test
52+
public void ensureEmailableReportsDontThrowExceptionsWhenMultipleClassesAreUsed() {
53+
TestNG testng = create(TestCaseSample.class, AnotherTestCaseSample.class);
54+
ExceptionAwareEmailableReporter reporter = new ExceptionAwareEmailableReporter();
55+
testng.addListener(reporter);
56+
testng.run();
57+
assertThat(reporter.hasError).isFalse();
58+
}
59+
60+
@Test
61+
public void ensureEmailableReportsDontThrowExceptionsWhenConfigsHaveErrors() {
62+
TestNG testng =
63+
create(
64+
TestCaseSample.class,
65+
TestCaseWithConfigProblemSample.class,
66+
AnotherTestCaseSample.class);
67+
ExceptionAwareEmailableReporter reporter = new ExceptionAwareEmailableReporter();
68+
testng.addListener(reporter);
69+
testng.run();
70+
assertThat(reporter.hasError).isFalse();
71+
}
72+
3873
@DataProvider(name = "getReporterInstances")
3974
public Object[][] getReporterInstances(Method method) {
4075
if (method.getName().toLowerCase().contains("jvmarguments")) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package test.reports.issue3038;
2+
3+
import org.testng.annotations.BeforeMethod;
4+
import org.testng.annotations.Test;
5+
6+
public class AnotherTestCaseSample {
7+
8+
@BeforeMethod
9+
public void beforeMethod() {}
10+
11+
@Test
12+
public void testHello() {}
13+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test.reports.issue3038;
2+
3+
import java.util.List;
4+
import org.testng.ISuite;
5+
import org.testng.reporters.EmailableReporter2;
6+
import org.testng.xml.XmlSuite;
7+
8+
public class ExceptionAwareEmailableReporter extends EmailableReporter2 {
9+
10+
public boolean hasError = false;
11+
12+
@Override
13+
public void generateReport(
14+
List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
15+
try {
16+
super.generateReport(xmlSuites, suites, outputDirectory);
17+
} catch (IllegalStateException e) {
18+
hasError = true;
19+
throw e;
20+
}
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package test.reports.issue3038;
2+
3+
import org.testng.annotations.BeforeMethod;
4+
import org.testng.annotations.Test;
5+
6+
public class TestCaseSample {
7+
8+
@BeforeMethod
9+
public void beforeMethod() {}
10+
11+
@Test
12+
public void testHello() {}
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package test.reports.issue3038;
2+
3+
import org.testng.annotations.BeforeMethod;
4+
import org.testng.annotations.Test;
5+
6+
public class TestCaseWithConfigProblemSample {
7+
8+
@BeforeMethod
9+
public void beforeMethod() {
10+
throw new RuntimeException("simulating a failure");
11+
}
12+
13+
@Test
14+
public void testHello() {}
15+
}

0 commit comments

Comments
 (0)