Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle exceptions in emailable Reporter #3042

Merged
merged 1 commit into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ protected List<ClassResult> groupResults(Set<ITestResult> 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));
Expand All @@ -764,7 +764,9 @@ protected List<ClassResult> groupResults(Set<ITestResult> 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();

Expand All @@ -773,7 +775,7 @@ protected List<ClassResult> groupResults(Set<ITestResult> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {}
}
Original file line number Diff line number Diff line change
@@ -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<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
try {
super.generateReport(xmlSuites, suites, outputDirectory);
} catch (IllegalStateException e) {
hasError = true;
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -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() {}
}
Original file line number Diff line number Diff line change
@@ -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() {}
}