Skip to content

Commit

Permalink
junit5: Print failure causes
Browse files Browse the repository at this point in the history
When the command-line summary prints a filtered stack trace for a
failure, it now also prints similarly filtered stack traces for causes.
  • Loading branch information
fmeum committed Apr 18, 2023
1 parent 4359934 commit c7113f7
Showing 1 changed file with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,32 @@ public void writeTo(PrintWriter writer) {
String className = LegacyReportingUtils.getClassName(testPlan, entry.getKey());

writer.printf("%d) %s (%s)%n", count, entry.getKey().getDisplayName(), className);
writeFilteredStackTrace(writer, failure.getCause(), className);

Throwable cause = failure.getCause();
StackTraceElement[] stackTrace = cause.getStackTrace();
// Find the last line in the stacktrace that matches the classname
count++;
}
}

int last = stackTrace.length - 1;
for (int i = 0; i < stackTrace.length; i++) {
if (className.equals(stackTrace[i].getClassName())) {
last = i;
}
}
private static void writeFilteredStackTrace(
PrintWriter writer, Throwable t, String testClassName) {
StackTraceElement[] stackTrace = t.getStackTrace();
// Find the last line in the stacktrace that matches the classname

writer.println(cause);
for (int i = 0; i <= last; i++) {
writer.println("\tat " + stackTrace[i]);
int last = stackTrace.length - 1;
for (int i = 0; i < stackTrace.length; i++) {
if (testClassName.equals(stackTrace[i].getClassName())) {
last = i;
}
}

count++;
writer.println(t);
for (int i = 0; i <= last; i++) {
writer.println("\tat " + stackTrace[i]);
}

if (t.getCause() != null) {
writer.print("Caused by: ");
writeFilteredStackTrace(writer, t.getCause(), testClassName);
}
}

Expand Down

0 comments on commit c7113f7

Please sign in to comment.