diff --git a/CHANGES.txt b/CHANGES.txt index 8e5e0915e..f21303d43 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,5 +1,6 @@ Current 7.10.0 +Fixed: GITHUB-3038: java.lang.IllegalStateException: Results per method should NOT have been empty (Krishnan Mahadevan) Fixed: GITHUB-3022: Remove deprecated JUnit related support in TestNG (Krishnan Mahadevan) 7.9.0 diff --git a/testng-core/src/main/java/org/testng/reporters/EmailableReporter2.java b/testng-core/src/main/java/org/testng/reporters/EmailableReporter2.java index 686b0ef04..e15bf7a30 100644 --- a/testng-core/src/main/java/org/testng/reporters/EmailableReporter2.java +++ b/testng-core/src/main/java/org/testng/reporters/EmailableReporter2.java @@ -750,7 +750,7 @@ protected List groupResults(Set results) { String className = result.getTestClass().getName(); if (!previousClassName.equals(className)) { // Different class implies different method - if (!resultsPerMethod.isEmpty()) { + if (resultsPerMethod.isEmpty()) { throw new IllegalStateException("Results per method should NOT have been empty"); } resultsPerClass.add(new MethodResult(resultsPerMethod)); @@ -764,7 +764,9 @@ protected List groupResults(Set results) { } else { String methodName = result.getMethod().getMethodName(); if (!previousMethodName.equals(methodName)) { - assert !resultsPerMethod.isEmpty(); + if (resultsPerMethod.isEmpty()) { + throw new IllegalStateException("Results per method should NOT have been empty"); + } resultsPerClass.add(new MethodResult(resultsPerMethod)); resultsPerMethod = Lists.newArrayList(); @@ -773,7 +775,7 @@ protected List groupResults(Set results) { } resultsPerMethod.add(result); } - if (!resultsPerMethod.isEmpty()) { + if (resultsPerMethod.isEmpty()) { throw new IllegalStateException("Results per method should NOT have been empty"); } resultsPerClass.add(new MethodResult(resultsPerMethod)); diff --git a/testng-core/src/test/java/test/reports/EmailableReporterTest.java b/testng-core/src/test/java/test/reports/EmailableReporterTest.java index 46e0c1038..2bcce4631 100644 --- a/testng-core/src/test/java/test/reports/EmailableReporterTest.java +++ b/testng-core/src/test/java/test/reports/EmailableReporterTest.java @@ -11,6 +11,10 @@ import org.testng.annotations.Test; import org.testng.reporters.EmailableReporter2; import test.SimpleBaseTest; +import test.reports.issue3038.AnotherTestCaseSample; +import test.reports.issue3038.ExceptionAwareEmailableReporter; +import test.reports.issue3038.TestCaseSample; +import test.reports.issue3038.TestCaseWithConfigProblemSample; public class EmailableReporterTest extends SimpleBaseTest { @Test(dataProvider = "getReporterInstances", priority = 1) @@ -35,6 +39,30 @@ public void testReportsNameCustomizationViaMainMethodInvocationAndJVMArguments( runTestViaMainMethod(clazzName, jvm); } + @Test + public void ensureEmailableReportsDontThrowExceptions() { + runTest(TestCaseSample.class); + } + + @Test + public void ensureEmailableReportsDontThrowExceptionsWhenMultipleClassesAreUsed() { + runTest(TestCaseSample.class, AnotherTestCaseSample.class); + } + + @Test + public void ensureEmailableReportsDontThrowExceptionsWhenConfigsHaveErrors() { + runTest( + TestCaseSample.class, TestCaseWithConfigProblemSample.class, AnotherTestCaseSample.class); + } + + private static void runTest(Class... classes) { + TestNG testng = create(classes); + ExceptionAwareEmailableReporter reporter = new ExceptionAwareEmailableReporter(); + testng.addListener(reporter); + testng.run(); + assertThat(reporter.hasError).isFalse(); + } + @DataProvider(name = "getReporterInstances") public Object[][] getReporterInstances(Method method) { if (method.getName().toLowerCase().contains("jvmarguments")) { diff --git a/testng-core/src/test/java/test/reports/issue3038/AnotherTestCaseSample.java b/testng-core/src/test/java/test/reports/issue3038/AnotherTestCaseSample.java new file mode 100644 index 000000000..c8c9c563a --- /dev/null +++ b/testng-core/src/test/java/test/reports/issue3038/AnotherTestCaseSample.java @@ -0,0 +1,13 @@ +package test.reports.issue3038; + +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class AnotherTestCaseSample { + + @BeforeMethod + public void beforeMethod() {} + + @Test + public void testHello() {} +} diff --git a/testng-core/src/test/java/test/reports/issue3038/ExceptionAwareEmailableReporter.java b/testng-core/src/test/java/test/reports/issue3038/ExceptionAwareEmailableReporter.java new file mode 100644 index 000000000..375acb5ec --- /dev/null +++ b/testng-core/src/test/java/test/reports/issue3038/ExceptionAwareEmailableReporter.java @@ -0,0 +1,22 @@ +package test.reports.issue3038; + +import java.util.List; +import org.testng.ISuite; +import org.testng.reporters.EmailableReporter2; +import org.testng.xml.XmlSuite; + +public class ExceptionAwareEmailableReporter extends EmailableReporter2 { + + public boolean hasError = false; + + @Override + public void generateReport( + List xmlSuites, List suites, String outputDirectory) { + try { + super.generateReport(xmlSuites, suites, outputDirectory); + } catch (IllegalStateException e) { + hasError = true; + throw e; + } + } +} diff --git a/testng-core/src/test/java/test/reports/issue3038/TestCaseSample.java b/testng-core/src/test/java/test/reports/issue3038/TestCaseSample.java new file mode 100644 index 000000000..9f6dceeec --- /dev/null +++ b/testng-core/src/test/java/test/reports/issue3038/TestCaseSample.java @@ -0,0 +1,13 @@ +package test.reports.issue3038; + +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class TestCaseSample { + + @BeforeMethod + public void beforeMethod() {} + + @Test + public void testHello() {} +} diff --git a/testng-core/src/test/java/test/reports/issue3038/TestCaseWithConfigProblemSample.java b/testng-core/src/test/java/test/reports/issue3038/TestCaseWithConfigProblemSample.java new file mode 100644 index 000000000..b68c8645d --- /dev/null +++ b/testng-core/src/test/java/test/reports/issue3038/TestCaseWithConfigProblemSample.java @@ -0,0 +1,15 @@ +package test.reports.issue3038; + +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class TestCaseWithConfigProblemSample { + + @BeforeMethod + public void beforeMethod() { + throw new RuntimeException("simulating a failure"); + } + + @Test + public void testHello() {} +}