Skip to content
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.
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 @@ -101,16 +101,19 @@ public static String readStackTrace(Throwable throwable) {
/**
* Prune the stack trace of the supplied {@link Throwable}.
*
* <p>Prune all {@linkplain StackTraceElement stack trace elements} up one
* of the supplied {@code classNames} are pruned. All subsequent elements
* in the stack trace will be retained.
* <p>Prune all {@linkplain StackTraceElement stack trace elements} up to one
* of the supplied {@code classNames}. All subsequent elements in the stack
* trace will be retained.
*
* <p>If the {@code classNames} do not match any of the stacktrace elements
* then the {@code org.junit}, {@code jdk.internal.reflect}, and
* {@code sun.reflect} packages are pruned.
*
* <p>Additionally, all elements prior to and including the first JUnit Platform
* Launcher call will be removed.
* <p>Additionally:
* <ul>
* <li>all elements prior to and including the first JUnit Platform Launcher call will be removed.
* <li>all elements prior to and including {@code org.junit.start} are kept.
* </ul>
*
* @param throwable the {@code Throwable} whose stack trace should be pruned;
* never {@code null}
Expand All @@ -126,14 +129,15 @@ public static void pruneStackTrace(Throwable throwable, List<String> classNames)

List<StackTraceElement> stackTrace = Arrays.asList(throwable.getStackTrace());
List<StackTraceElement> prunedStackTrace = new ArrayList<>();
List<StackTraceElement> junitStartStackTrace = new ArrayList<>(0);

Collections.reverse(stackTrace);

for (int i = 0; i < stackTrace.size(); i++) {
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 @@ -142,7 +146,9 @@ public static void pruneStackTrace(Throwable throwable, List<String> classNames)
break;
}
else if (className.startsWith(JUNIT_START_PACKAGE_PREFIX)) {
junitStartStackTrace.addAll(prunedStackTrace);
prunedStackTrace.clear();
junitStartStackTrace.add(element);
}
else if (className.startsWith(JUNIT_PLATFORM_LAUNCHER_PACKAGE_PREFIX)) {
prunedStackTrace.clear();
Expand All @@ -152,10 +158,22 @@ else if (STACK_TRACE_ELEMENT_FILTER.test(className)) {
}
}

if (!junitStartStackTrace.isEmpty()) {
junitStartStackTrace.addAll(prunedStackTrace);
prunedStackTrace = junitStartStackTrace;
}

Collections.reverse(prunedStackTrace);
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,30 @@ void pruneStackTraceOfEverythingPriorToFirstLauncherCall() {
.noneMatch(element -> element.toString().contains("org.example.Class.method(file:123)"));
}

@Test
void pruneStackTraceRetainsStackFramesFromJUnitStart() {
// Non-test class frames from org.junit are filtered.
var testClassName = "com.example.project.HelloTest";
var testFileName = "HelloTest.java";

var exception = new JUnitException("expected");
var stackTrace = exception.getStackTrace();
var extendedStacktrace = Arrays.copyOfRange(stackTrace, 0, stackTrace.length + 2);
extendedStacktrace[0] = new StackTraceElement(testClassName, "stringLength", testFileName, 10);
extendedStacktrace[stackTrace.length] = new StackTraceElement("org.junit.start.JUnit", "run", "JUnit.java", 3);
extendedStacktrace[stackTrace.length + 1] = new StackTraceElement(testClassName, "main", testFileName, 5);
exception.setStackTrace(extendedStacktrace);

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

assertThat(exception.getStackTrace()) //
.extracting(StackTraceElement::toString) //
.containsExactly( //
"com.example.project.HelloTest.stringLength(HelloTest.java:10)", //
"org.junit.start.JUnit.run(JUnit.java:3)", //
"com.example.project.HelloTest.main(HelloTest.java:5)");
}

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