Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static void pruneStackTrace(Throwable throwable, List<String> classNames)
StackTraceElement element = stackTrace.get(i);
String className = element.getClassName();

if (classNames.contains(className)) {
if (classNames.contains(className) && !includesJunitStart(stackTrace, i + 1)) {
// We found the test
// everything before that is not informative.
prunedStackTrace.clear();
Expand All @@ -156,6 +156,13 @@ else if (STACK_TRACE_ELEMENT_FILTER.test(className)) {
throwable.setStackTrace(prunedStackTrace.toArray(new StackTraceElement[0]));
}

private static boolean includesJunitStart(List<StackTraceElement> stackTrace, int fromIndex) {
return stackTrace.stream() //
.skip(fromIndex) //
.map(StackTraceElement::getClassName) //
.anyMatch(className -> className.startsWith(JUNIT_START_PACKAGE_PREFIX));
}

/**
* Find all causes and suppressed exceptions in the stack trace of the
* supplied {@link Throwable}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.platform.commons.util.ExceptionUtils.throwAsUncheckedException;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -104,6 +105,25 @@ void pruneStackTraceOfEverythingPriorToFirstLauncherCall() {
.noneMatch(element -> element.toString().contains("org.example.Class.method(file:123)"));
}

@Test
void pruneStackTraceOfJUnitStart() {
var testClassName = ExceptionUtilsTests.class.getCanonicalName();

var exception = new JUnitException("expected");
var stackTrace = exception.getStackTrace();

var extendedStacktrace = Arrays.copyOfRange(stackTrace, 0, stackTrace.length + 2);
extendedStacktrace[stackTrace.length] = new StackTraceElement("org.junit.start.JUnit", "run", "JUnit.class", 3);
extendedStacktrace[stackTrace.length + 1] = new StackTraceElement(testClassName, "main",
"ExceptionUtilsTest.class", 5);
exception.setStackTrace(extendedStacktrace);

pruneStackTrace(exception, List.of(testClassName));

assertThat(exception.getStackTrace()) //
.noneMatch(element -> element.toString().contains(testClassName + ".main(file:5)"));
}

@Test
void findSuppressedExceptionsAndCausesOfThrowable() {
Throwable t1 = new Throwable("#1");
Expand Down