use of org.junit.platform.engine.UniqueId in project junit5 by junit-team.
the class TestPlan method getTestIdentifier.
/**
* Get the {@link TestIdentifier} with the supplied unique ID.
*
* @param uniqueId the unique ID to look up the identifier for; never
* {@code null} or blank
* @return the identifier with the supplied unique ID; never {@code null}
* @throws PreconditionViolationException if no {@code TestIdentifier}
* with the supplied unique ID is present in this test plan
*/
public TestIdentifier getTestIdentifier(String uniqueId) throws PreconditionViolationException {
Preconditions.notBlank(uniqueId, "unique ID must not be null or blank");
UniqueId uniqueIdObject = UniqueId.parse(uniqueId);
Preconditions.condition(allIdentifiers.containsKey(uniqueIdObject), () -> "No TestIdentifier with unique ID [" + uniqueId + "] has been added to this TestPlan.");
return allIdentifiers.get(uniqueIdObject);
}
use of org.junit.platform.engine.UniqueId in project junit5 by junit-team.
the class EngineDiscoveryOrchestrator method discoverEngineRoot.
private TestDescriptor discoverEngineRoot(TestEngine testEngine, LauncherDiscoveryRequest request, LauncherDiscoveryListener listener, Function<String, UniqueId> uniqueIdCreator) {
UniqueId uniqueEngineId = uniqueIdCreator.apply(testEngine.getId());
try {
listener.engineDiscoveryStarted(uniqueEngineId);
TestDescriptor engineRoot = testEngine.discover(request, uniqueEngineId);
discoveryResultValidator.validate(testEngine, engineRoot);
listener.engineDiscoveryFinished(uniqueEngineId, EngineDiscoveryResult.successful());
return engineRoot;
} catch (Throwable throwable) {
UnrecoverableExceptions.rethrowIfUnrecoverable(throwable);
String message = String.format("TestEngine with ID '%s' failed to discover tests", testEngine.getId());
JUnitException cause = new JUnitException(message, throwable);
listener.engineDiscoveryFinished(uniqueEngineId, EngineDiscoveryResult.failed(cause));
return new EngineDiscoveryErrorDescriptor(uniqueEngineId, testEngine, cause);
}
}
use of org.junit.platform.engine.UniqueId in project junit5 by junit-team.
the class DefaultLauncherTests method prunesTestDescriptorsAfterApplyingPostDiscoveryFilters.
@Test
void prunesTestDescriptorsAfterApplyingPostDiscoveryFilters() {
var engine = new TestEngineSpy() {
@Override
public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId uniqueId) {
super.discover(discoveryRequest, uniqueId);
var engineDescriptor = new TestDescriptorStub(uniqueId, uniqueId.toString());
var containerDescriptor = new TestDescriptorStub(uniqueId.append("container", "a"), "container") {
@Override
public Type getType() {
return Type.CONTAINER;
}
};
containerDescriptor.addChild(new TestDescriptorStub(containerDescriptor.getUniqueId().append("test", "b"), "test"));
engineDescriptor.addChild(containerDescriptor);
return engineDescriptor;
}
};
var launcher = createLauncher(engine);
var testPlan = launcher.discover(request().filters((PostDiscoveryFilter) testDescriptor -> FilterResult.includedIf(testDescriptor.isContainer())).build());
assertThat(testPlan.getRoots()).hasSize(1);
var engineIdentifier = getOnlyElement(testPlan.getRoots());
assertThat(testPlan.getChildren(engineIdentifier)).isEmpty();
}
use of org.junit.platform.engine.UniqueId in project junit5 by junit-team.
the class DefaultLauncherTests method discoverErrorTestDescriptorForEngineThatThrowsInDiscoveryPhase.
@ParameterizedTest
@ValueSource(classes = { Error.class, RuntimeException.class })
void discoverErrorTestDescriptorForEngineThatThrowsInDiscoveryPhase(Class<? extends Throwable> throwableClass) {
TestEngine engine = new TestEngineStub("my-engine-id") {
@Override
public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId uniqueId) {
try {
var constructor = throwableClass.getDeclaredConstructor(String.class);
throw ExceptionUtils.throwAsUncheckedException(constructor.newInstance("ignored"));
} catch (Exception ignored) {
return null;
}
}
};
var launcher = createLauncher(engine);
var discoveryListener = mock(LauncherDiscoveryListener.class);
var request = //
request().listeners(//
discoveryListener).configurationParameter(DEFAULT_DISCOVERY_LISTENER_CONFIGURATION_PROPERTY_NAME, //
"logging").build();
var testPlan = launcher.discover(request);
assertThat(testPlan.getRoots()).hasSize(1);
var engineIdentifier = getOnlyElement(testPlan.getRoots());
assertThat(getOnlyElement(testPlan.getRoots()).getDisplayName()).isEqualTo("my-engine-id");
verify(discoveryListener).launcherDiscoveryStarted(request);
verify(discoveryListener).launcherDiscoveryFinished(request);
assertDiscoveryFailed(engine, discoveryListener);
var listener = mock(TestExecutionListener.class);
launcher.execute(testPlan, listener);
var testExecutionResult = ArgumentCaptor.forClass(TestExecutionResult.class);
verify(listener).executionStarted(engineIdentifier);
verify(listener).executionFinished(eq(engineIdentifier), testExecutionResult.capture());
assertThat(testExecutionResult.getValue().getThrowable()).isPresent();
//
assertThat(testExecutionResult.getValue().getThrowable().get()).hasMessage("TestEngine with ID 'my-engine-id' failed to discover tests");
}
use of org.junit.platform.engine.UniqueId in project junit5 by junit-team.
the class SuiteEngineTests method selectMethodsInTestPlanByUniqueId.
@Test
void selectMethodsInTestPlanByUniqueId() {
// @formatter:off
UniqueId uniqueId = UniqueId.forEngine(ENGINE_ID).append(SuiteTestDescriptor.SEGMENT_TYPE, MultipleSuite.class.getName()).append("engine", JupiterEngineDescriptor.ENGINE_ID).append(ClassTestDescriptor.SEGMENT_TYPE, MultipleTestsTestCase.class.getName()).append(TestMethodTestDescriptor.SEGMENT_TYPE, "test()");
UniqueId uniqueId2 = UniqueId.forEngine(ENGINE_ID).append(SuiteTestDescriptor.SEGMENT_TYPE, MultipleSuite.class.getName()).append("engine", JupiterEngineDescriptor.ENGINE_ID).append(ClassTestDescriptor.SEGMENT_TYPE, MultipleTestsTestCase.class.getName()).append(TestMethodTestDescriptor.SEGMENT_TYPE, "test2()");
EngineTestKit.engine(ENGINE_ID).selectors(selectUniqueId(uniqueId)).selectors(selectUniqueId(uniqueId2)).execute().testEvents().assertThatEvents().haveExactly(2, event(test(MultipleSuite.class.getName()), finishedSuccessfully())).haveExactly(2, event(test(MultipleTestsTestCase.class.getName()), finishedSuccessfully()));
// @formatter:on
}
Aggregations