Skip to content

Commit

Permalink
Add tests for ExceptionUtils.pruneStackTrace
Browse files Browse the repository at this point in the history
  • Loading branch information
juliette-derancourt committed Apr 28, 2023
1 parent 0777800 commit 9de179a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static void pruneStackTrace(Throwable throwable, Predicate<String> stackT
if (name.startsWith(JUNIT_PLATFORM_LAUNCHER_PACKAGE_PREFIX)) {
prunedStackTrace.clear();
}
if (stackTraceElementFilter.test(name) || ALWAYS_INCLUDED_STACK_TRACE_ELEMENTS.contains(name)) {
else if (stackTraceElementFilter.test(name) || ALWAYS_INCLUDED_STACK_TRACE_ELEMENTS.contains(name)) {
prunedStackTrace.add(element);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,6 @@ void shouldAlwaysKeepJupiterAssumptionStackTraceElement() {
""");
}

@Test
void shouldPruneEverythingAfterLastLauncherStackTraceElement() {
EngineExecutionResults results = EngineTestKit.engine("junit-jupiter") //
.configurationParameter("junit.platform.stacktrace.pruning.enabled", "true") //
.selectors(selectClass(FailingAssertionWithCustomStackTraceElementTestCase.class)) //
.execute();

List<StackTraceElement> stackTrace = extractStackTrace(results);

assertStackTraceDoesNotContain(stackTrace, "someClass.someMethod(someFile:123)");
}

private static List<StackTraceElement> extractStackTrace(EngineExecutionResults results) {
var failedTestEvent = results.testEvents().failed().list().get(0);
var testResult = failedTestEvent.getRequiredPayload(TestExecutionResult.class);
Expand Down Expand Up @@ -184,21 +172,4 @@ void test() {

}

static class FailingAssertionWithCustomStackTraceElementTestCase {

@Test
void test() {
try {
Assertions.fail();
}
catch (Throwable throwable) {
StackTraceElement[] stackTrace = throwable.getStackTrace();
stackTrace[stackTrace.length - 1] = new StackTraceElement("someClass", "someMethod", "someFile", 123);
throwable.setStackTrace(stackTrace);
throw throwable;
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@
package org.junit.platform.commons.util;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.platform.commons.util.ExceptionUtils.readStackTrace;
import static org.junit.platform.commons.util.ExceptionUtils.throwAsUncheckedException;

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

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.JUnitException;
import org.junit.platform.commons.PreconditionViolationException;
import org.opentest4j.AssertionFailedError;

/**
* Unit tests for {@link ExceptionUtils}.
Expand Down Expand Up @@ -62,4 +69,76 @@ void readStackTraceForLocalJUnitException() {
}
}

@Test
void pruneStackTraceOfCallsFromSpecificPackage() {
try {
throw new JUnitException("expected");
}
catch (JUnitException e) {
ExceptionUtils.pruneStackTrace(e, element -> !element.startsWith("org.junit."));
assertThat(e.getStackTrace()) //
.noneMatch(element -> element.toString().contains("org.junit."));
}
}

@Test
void pruneStackTraceOfEverythingExceptJupiterAssertions() {
try {
Assertions.fail();
}
catch (AssertionFailedError e) {
ExceptionUtils.pruneStackTrace(e, element -> false);
assertStackTraceMatch(e.getStackTrace(), "\\Qorg.junit.jupiter.api.Assertions.fail(Assertions.java:\\E.+");
}
}

@Test
void pruneStackTraceOfEverythingExceptJupiterAssumptions() {
try {
Assumptions.assumeTrue(() -> {
throw new JUnitException("expected");
});
}
catch (JUnitException e) {
ExceptionUtils.pruneStackTrace(e, element -> false);
assertStackTraceMatch(e.getStackTrace(),
"\\Qorg.junit.jupiter.api.Assumptions.assumeTrue(Assumptions.java:\\E.+");
}
}

@Test
void pruneStackTraceOfAllLauncherCalls() {
try {
throw new JUnitException("expected");
}
catch (JUnitException e) {
ExceptionUtils.pruneStackTrace(e, element -> true);
assertThat(e.getStackTrace()) //
.noneMatch(element -> element.toString().contains("org.junit.platform.launcher."));
}
}

@Test
void pruneStackTraceOfEverythingPriorToFirstLauncherCall() {
try {
throw new JUnitException("expected");
}
catch (JUnitException e) {
StackTraceElement[] stackTrace = e.getStackTrace();
stackTrace[stackTrace.length - 1] = new StackTraceElement("org.example.Class", "method", "file", 123);
e.setStackTrace(stackTrace);

ExceptionUtils.pruneStackTrace(e, element -> true);
assertThat(e.getStackTrace()) //
.noneMatch(element -> element.toString().contains("org.example.Class.method(file:123)"));
}
}

private static void assertStackTraceMatch(StackTraceElement[] stackTrace, String expectedLines) {
List<String> stackStraceAsLines = Arrays.stream(stackTrace) //
.map(StackTraceElement::toString) //
.collect(Collectors.toList());
assertLinesMatch(expectedLines.lines().toList(), stackStraceAsLines);
}

}

0 comments on commit 9de179a

Please sign in to comment.