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

GITHUB-2830 - Failsafe parameter.toString #2831

Merged
merged 1 commit into from Nov 10, 2022
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
3 changes: 2 additions & 1 deletion CHANGES.txt
Expand Up @@ -9,7 +9,8 @@ Fixed: GITHUB-2800: Running Test Classes with Inherited @Factory and @DataProvid
New: Ability to provide custom error message for assertThrows\expectThrows methods (Anatolii Yuzhakov)
Fixed: GITHUB-2780: Use SpotBugs instead of abandoned FindBugs
Fixed: GITHUB-2801: JUnitReportReporter is too slow
Fixed: GITHUB-2807: buildStackTrace should be fail-safe
Fixed: GITHUB-2807: buildStackTrace should be fail-safe (Sergey Chernov)
Fixed: GITHUB-2830: TestHTMLReporter parameter toString should be fail-safe (Sergey Chernov)
Fixed: GITHUB-2798: Parallel executions coupled with retry analyzer results in duplicate retry analyzer instances being created (Krishnan Mahadevan)

7.6.1
Expand Down
Expand Up @@ -123,7 +123,20 @@ public static void generateTable(
if (j > 0) {
pw.append(", ");
}
pw.append(parameters[j] == null ? "null" : parameters[j].toString());
if (parameters[j] == null) {
pw.append("null");
} else {
String parameterToString;
try {
parameterToString = parameters[j].toString();
} catch (RuntimeException | Error e) {
log(e.toString());
// failover in case parameter toString() cannot be evaluated
parameterToString =
parameters[j].getClass().getName() + "@" + System.identityHashCode(parameters[j]);
}
pw.append(parameterToString);
}
}
}

Expand Down
@@ -0,0 +1,49 @@
package org.testng.reporters;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collections;
import java.util.List;
import org.testng.ITestClass;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.annotations.Test;
import org.testng.internal.TestResult;

public class TestHTMLReporterTest {

@Test(description = "GITHUB-2830")
public void generateTableParametersToStringShouldBeFailsafe() {
ITestClass testClass = mock(ITestClass.class);
when(testClass.getName()).thenReturn("testClass");

ITestNGMethod testNGMethod = mock(ITestNGMethod.class);
when(testNGMethod.getMethodName()).thenReturn("testMethod");
when(testNGMethod.getTestClass()).thenReturn(testClass);

TestResult testResult = TestResult.newEmptyTestResult();
testResult.setMethod(testNGMethod);

testResult.setParameters(new Object[] {new ThrowingOnToString()});

List<ITestResult> tests = Collections.singletonList(testResult);

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
TestHTMLReporter.generateTable(pw, "title", tests, "cssClass", (t1, t2) -> 0);

assertThat(sw.toString())
.contains("Parameters: org.testng.reporters.TestHTMLReporterTest$ThrowingOnToString@");
}

private static class ThrowingOnToString {
@Override
public String toString() {
throw new IllegalStateException("Cannot calculate toString");
}
}
}