diff --git a/junit-platform-launcher/src/main/java/org/junit/platform/launcher/core/DefaultLauncher.java b/junit-platform-launcher/src/main/java/org/junit/platform/launcher/core/DefaultLauncher.java index ecefb991bb8..8bb8b1d078d 100644 --- a/junit-platform-launcher/src/main/java/org/junit/platform/launcher/core/DefaultLauncher.java +++ b/junit-platform-launcher/src/main/java/org/junit/platform/launcher/core/DefaultLauncher.java @@ -11,6 +11,7 @@ package org.junit.platform.launcher.core; import java.util.HashSet; +import java.util.Optional; import java.util.Set; import java.util.logging.Logger; @@ -111,17 +112,32 @@ private Root discoverRoot(LauncherDiscoveryRequest discoveryRequest, String phas LOG.fine(() -> String.format("Discovering tests during Launcher %s phase in engine '%s'.", phase, testEngine.getId())); - UniqueId uniqueEngineId = UniqueId.forEngine(testEngine.getId()); + Optional engineRoot = discoverRoot(testEngine, discoveryRequest); + engineRoot.ifPresent(rootDescriptor -> root.add(testEngine, rootDescriptor)); + } + root.applyPostDiscoveryFilters(discoveryRequest); + root.prune(); + return root; + } + + private Optional discoverRoot(TestEngine testEngine, LauncherDiscoveryRequest discoveryRequest) { + UniqueId uniqueEngineId = UniqueId.forEngine(testEngine.getId()); + try { TestDescriptor engineRoot = testEngine.discover(discoveryRequest, uniqueEngineId); Preconditions.notNull(engineRoot, () -> String.format( "The discover() method for TestEngine with ID '%s' must return a non-null root TestDescriptor.", testEngine.getId())); - root.add(testEngine, engineRoot); + return Optional.of(engineRoot); + } + catch (Throwable throwable) { + // re-throw our own exceptions + if (throwable instanceof JUnitException) { + throw throwable; + } + // TODO Collect and/or log runtime exception. + return Optional.empty(); } - root.applyPostDiscoveryFilters(discoveryRequest); - root.prune(); - return root; } private void execute(Root root, ConfigurationParameters configurationParameters, diff --git a/platform-tests/src/test/java/org/junit/platform/launcher/core/DefaultLauncherTests.java b/platform-tests/src/test/java/org/junit/platform/launcher/core/DefaultLauncherTests.java index 3343d5362e9..97b6ed08b5e 100644 --- a/platform-tests/src/test/java/org/junit/platform/launcher/core/DefaultLauncherTests.java +++ b/platform-tests/src/test/java/org/junit/platform/launcher/core/DefaultLauncherTests.java @@ -127,6 +127,36 @@ public TestDescriptor discover(org.junit.platform.engine.EngineDiscoveryRequest engine.getId()); } + @Test + void discoverTestPlanForEngineThatThrowsAnErrorInDiscoverPhase() { + TestEngine engine = new TestEngineStub() { + + @Override + public TestDescriptor discover(org.junit.platform.engine.EngineDiscoveryRequest discoveryRequest, + UniqueId uniqueId) { + throw new Error("ignored"); + } + }; + + TestPlan testPlan = createLauncher(engine).discover(request().build()); + assertThat(testPlan.getRoots()).hasSize(0); + } + + @Test + void discoverTestPlanForEngineThatThrowsRuntimeExceptionInDiscoverPhase() { + TestEngine engine = new TestEngineStub() { + + @Override + public TestDescriptor discover(org.junit.platform.engine.EngineDiscoveryRequest discoveryRequest, + UniqueId uniqueId) { + throw new RuntimeException("ignored"); + } + }; + + TestPlan testPlan = createLauncher(engine).discover(request().build()); + assertThat(testPlan.getRoots()).hasSize(0); + } + @Test void discoverTestPlanForSingleEngine() { DemoHierarchicalTestEngine engine = new DemoHierarchicalTestEngine("myEngine");