diff --git a/pom.xml b/pom.xml index 5ef9a9a5..7f39599c 100644 --- a/pom.xml +++ b/pom.xml @@ -46,9 +46,16 @@ org.junit.jupiter junit-jupiter - 5.9.0 + 5.10.2 test + + org.junit.platform + junit-platform-launcher + 1.10.2 + test + + diff --git a/src/test/java/se/kth/spork/TestOutputHandler.java b/src/test/java/se/kth/spork/TestOutputHandler.java new file mode 100644 index 00000000..751def6d --- /dev/null +++ b/src/test/java/se/kth/spork/TestOutputHandler.java @@ -0,0 +1,38 @@ +package se.kth.spork; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.platform.engine.TestExecutionResult; +import org.junit.platform.launcher.TestExecutionListener; +import org.junit.platform.launcher.TestIdentifier; + +public class TestOutputHandler implements TestExecutionListener { + private final PrintStream stdout = System.out; + private final PrintStream stderr = System.err; + + private final ByteArrayOutputStream capturedStdout = new ByteArrayOutputStream(); + private final ByteArrayOutputStream capturedStderr = new ByteArrayOutputStream(); + + @Override + public void executionStarted(TestIdentifier testIdentifier) { + System.setOut(new PrintStream(capturedStdout)); + System.setErr(new PrintStream(capturedStderr)); + } + + @Override + public void executionFinished( + TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { + System.setOut(stdout); + System.setErr(stderr); + if (testExecutionResult.getStatus().equals(TestExecutionResult.Status.FAILED)) { + System.out.printf( + "### OUTPUT FROM %s (%s) ###", + testIdentifier.getLegacyReportingName(), testIdentifier.getDisplayName()); + System.out.println(capturedStdout.toString()); + System.out.println(capturedStderr.toString()); + } + + capturedStdout.reset(); + capturedStderr.reset(); + } +} diff --git a/src/test/resources/META-INF/services/org.junit.platform.launcher.TestExecutionListener b/src/test/resources/META-INF/services/org.junit.platform.launcher.TestExecutionListener new file mode 100644 index 00000000..17181452 --- /dev/null +++ b/src/test/resources/META-INF/services/org.junit.platform.launcher.TestExecutionListener @@ -0,0 +1 @@ +se.kth.spork.TestOutputHandler