Skip to content

Commit f45f5b3

Browse files
authored
preserve test failures when stack traces cannot be fully rendered (fixes #1035, via #1335)
1 parent 79e85c5 commit f45f5b3

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

allure-java-commons/src/main/java/io/qameta/allure/util/ResultsUtils.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,18 @@ private static String getRealThreadName() {
870870

871871
private static String getStackTraceAsString(final Throwable throwable) {
872872
final StringWriter stringWriter = new StringWriter();
873-
throwable.printStackTrace(new PrintWriter(stringWriter));
873+
try {
874+
throwable.printStackTrace(new PrintWriter(stringWriter));
875+
} catch (RuntimeException e) {
876+
if (stringWriter.getBuffer().length() == 0) {
877+
stringWriter.append(throwable.getClass().getName());
878+
}
879+
stringWriter
880+
.append(System.lineSeparator())
881+
.append("[Unable to render the complete stack trace: ")
882+
.append(e.getClass().getName())
883+
.append(']');
884+
}
874885
return stringWriter.toString();
875886
}
876887

allure-java-commons/src/test/java/io/qameta/allure/ResultsUtilsTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.opentest4j.AssertionFailedError;
2828
import org.opentest4j.ValueWrapper;
2929

30+
import java.io.IOException;
3031
import java.io.Serializable;
3132
import java.lang.annotation.Annotation;
3233
import java.util.List;
@@ -48,6 +49,10 @@
4849
import static io.qameta.allure.util.ResultsUtils.createTmsLink;
4950
import static io.qameta.allure.util.ResultsUtils.getLinkTypePatternPropertyName;
5051
import static org.assertj.core.api.Assertions.assertThat;
52+
import static org.junit.jupiter.api.Assertions.assertThrows;
53+
import static org.mockito.Mockito.mock;
54+
import static org.mockito.Mockito.when;
55+
5156
@ExtendWith(SystemPropertyExtension.class)
5257
class ResultsUtilsTest {
5358

@@ -399,6 +404,27 @@ void shouldExtractActualAndExpectedFromOpenTest4jAssertionFailedError() {
399404
assertThat(details.getExpected()).startsWith("expected value (");
400405
}
401406

407+
@Test
408+
@Issue("1035")
409+
void shouldCreateStatusDetailsWhenNestedStackTraceCannotBeRendered() {
410+
final RuntimeException cause = mock(RuntimeException.class);
411+
when(cause.getSuppressed()).thenReturn(null);
412+
final AssertionFailedError error = assertThrows(
413+
AssertionFailedError.class,
414+
() -> assertThrows(IOException.class, () -> {
415+
throw cause;
416+
})
417+
);
418+
419+
final StatusDetails details = getStatusDetailsFor("malformed nested throwable", error);
420+
421+
assertThat(details.getMessage()).startsWith("Unexpected exception type thrown");
422+
assertThat(details.getTrace())
423+
.contains(AssertionFailedError.class.getName())
424+
.contains("Caused by:")
425+
.contains("[Unable to render the complete stack trace: java.lang.NullPointerException]");
426+
}
427+
402428
@Test
403429
void shouldUseOpenTest4jValueWrapperToString() {
404430
final AssertionFailedError error = new AssertionFailedError(

0 commit comments

Comments
 (0)