From 015f7273ed0ff2319cb837378a35579272def398 Mon Sep 17 00:00:00 2001 From: Marc Philipp Date: Mon, 21 Oct 2024 15:10:05 +0200 Subject: [PATCH] Capture TestPlan in separate listener --- .../console/tasks/ConsoleTestExecutor.java | 25 +++++++++++++------ .../listeners/SummaryGeneratingListener.java | 7 ------ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/junit-platform-console/src/main/java/org/junit/platform/console/tasks/ConsoleTestExecutor.java b/junit-platform-console/src/main/java/org/junit/platform/console/tasks/ConsoleTestExecutor.java index 57d92922341d..b8c8bfdec0e3 100644 --- a/junit-platform-console/src/main/java/org/junit/platform/console/tasks/ConsoleTestExecutor.java +++ b/junit-platform-console/src/main/java/org/junit/platform/console/tasks/ConsoleTestExecutor.java @@ -49,8 +49,6 @@ public class ConsoleTestExecutor { private final TestConsoleOutputOptions outputOptions; private final Supplier launcherSupplier; - private TestPlan testPlanListeners; - public ConsoleTestExecutor(TestDiscoveryOptions discoveryOptions, TestConsoleOutputOptions outputOptions) { this(discoveryOptions, outputOptions, LauncherFactory::create); } @@ -101,12 +99,13 @@ private TestExecutionSummary executeTests(PrintWriter out, Optional report Launcher launcher = launcherSupplier.get(); SummaryGeneratingListener summaryListener = registerListeners(out, reportsDir, launcher); LauncherDiscoveryRequest discoveryRequest = new DiscoveryRequestCreator().toDiscoveryRequest(discoveryOptions); - launcher.execute(discoveryRequest); + + TestPlanCapturer testPlanCapturer = new TestPlanCapturer(); + launcher.execute(discoveryRequest, testPlanCapturer); + TestExecutionSummary summary = summaryListener.getSummary(); if (summary.getTotalFailureCount() > 0 || outputOptions.getDetails() != Details.NONE) { - //get testPlan from summaryListener - testPlanListeners = summaryListener.getTestPlan(); - printSummary(summary, out); + printSummary(summary, testPlanCapturer.testPlan, out); } return summary; @@ -180,7 +179,7 @@ private Optional createXmlWritingListener(PrintWriter out return reportsDir.map(it -> new LegacyXmlReportGeneratingListener(it, out)); } - private void printSummary(TestExecutionSummary summary, PrintWriter out) { + private void printSummary(TestExecutionSummary summary, TestPlan testPlan, PrintWriter out) { // Otherwise the failures have already been printed in detail if (EnumSet.of(Details.NONE, Details.SUMMARY, Details.TREE).contains(outputOptions.getDetails())) { summary.printFailuresTo(out); @@ -198,7 +197,7 @@ private void printSummary(TestExecutionSummary summary, PrintWriter out) { ValueWrapper actual = assertionFailedError.getActual(); //apply diff function if (isCharSequence(expected) && isCharSequence(actual)) { - new DiffPrinter(testPlanListeners).printDiff(out, expected.getStringRepresentation(), + new DiffPrinter(testPlan).printDiff(out, expected.getStringRepresentation(), actual.getStringRepresentation(), failure.getTestIdentifier()); } } @@ -215,4 +214,14 @@ private boolean isCharSequence(ValueWrapper value) { public interface Factory { ConsoleTestExecutor create(TestDiscoveryOptions discoveryOptions, TestConsoleOutputOptions outputOptions); } + + private static class TestPlanCapturer implements TestExecutionListener { + + private TestPlan testPlan; + + @Override + public void testPlanExecutionStarted(TestPlan testPlan) { + this.testPlan = testPlan; + } + } } diff --git a/junit-platform-launcher/src/main/java/org/junit/platform/launcher/listeners/SummaryGeneratingListener.java b/junit-platform-launcher/src/main/java/org/junit/platform/launcher/listeners/SummaryGeneratingListener.java index f881769fc07b..c01c09af2bf9 100644 --- a/junit-platform-launcher/src/main/java/org/junit/platform/launcher/listeners/SummaryGeneratingListener.java +++ b/junit-platform-launcher/src/main/java/org/junit/platform/launcher/listeners/SummaryGeneratingListener.java @@ -46,13 +46,6 @@ public TestExecutionSummary getSummary() { return this.summary; } - /** - * Get the testPlan of this listener. - */ - public TestPlan getTestPlan() { - return testPlan; - } - @Override public void testPlanExecutionStarted(TestPlan testPlan) { this.testPlan = testPlan;