Skip to content

Commit

Permalink
Handle exceptions in test discovery phase
Browse files Browse the repository at this point in the history
State: work-in-progress

Addresses: #750
  • Loading branch information
sormuras committed Apr 10, 2017
1 parent c7a4ade commit 56af105
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<TestDescriptor> engineRoot = discoverRoot(testEngine, discoveryRequest);
engineRoot.ifPresent(rootDescriptor -> root.add(testEngine, rootDescriptor));
}
root.applyPostDiscoveryFilters(discoveryRequest);
root.prune();
return root;
}

private Optional<TestDescriptor> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 56af105

Please sign in to comment.