Search in sources :

Example 51 with UniqueId

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);
}
Also used : UniqueId(org.junit.platform.engine.UniqueId)

Example 52 with UniqueId

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);
    }
}
Also used : UniqueId(org.junit.platform.engine.UniqueId) JUnitException(org.junit.platform.commons.JUnitException) TestDescriptor(org.junit.platform.engine.TestDescriptor)

Example 53 with UniqueId

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();
}
Also used : DemoHierarchicalTestEngine(org.junit.platform.engine.support.hierarchical.DemoHierarchicalTestEngine) TestPlan(org.junit.platform.launcher.TestPlan) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) TestEngineStub(org.junit.platform.fakes.TestEngineStub) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LauncherDiscoveryListener(org.junit.platform.launcher.LauncherDiscoveryListener) TestEngineSpy(org.junit.platform.fakes.TestEngineSpy) LauncherDiscoveryRequestBuilder.request(org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request) TestDescriptorStub(org.junit.platform.fakes.TestDescriptorStub) PostDiscoveryFilter(org.junit.platform.launcher.PostDiscoveryFilter) JUnitException(org.junit.platform.commons.JUnitException) LogRecord(java.util.logging.LogRecord) Test(org.junit.jupiter.api.Test) LogRecordListener(org.junit.platform.commons.logging.LogRecordListener) TestIdentifier(org.junit.platform.launcher.TestIdentifier) PostDiscoveryFilterStub(org.junit.platform.launcher.PostDiscoveryFilterStub) SummaryGeneratingListener(org.junit.platform.launcher.listeners.SummaryGeneratingListener) DiscoverySelectors.selectUniqueId(org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId) PreconditionViolationException(org.junit.platform.commons.PreconditionViolationException) Mockito.inOrder(org.mockito.Mockito.inOrder) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TestDescriptor(org.junit.platform.engine.TestDescriptor) Mockito.mock(org.mockito.Mockito.mock) TestExecutionListener(org.junit.platform.launcher.TestExecutionListener) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DEFAULT_DISCOVERY_LISTENER_CONFIGURATION_PROPERTY_NAME(org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.DEFAULT_DISCOVERY_LISTENER_CONFIGURATION_PROPERTY_NAME) EngineDescriptor(org.junit.platform.engine.support.descriptor.EngineDescriptor) EngineFilter.excludeEngines(org.junit.platform.launcher.EngineFilter.excludeEngines) FilterResult(org.junit.platform.engine.FilterResult) TestEngine(org.junit.platform.engine.TestEngine) TestExecutionResult(org.junit.platform.engine.TestExecutionResult) Level(java.util.logging.Level) EngineDiscoveryResult(org.junit.platform.launcher.EngineDiscoveryResult) TestExecutionResult.successful(org.junit.platform.engine.TestExecutionResult.successful) ArgumentCaptor(org.mockito.ArgumentCaptor) LauncherFactoryForTestingPurposesOnly.createLauncher(org.junit.platform.launcher.core.LauncherFactoryForTestingPurposesOnly.createLauncher) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EngineDiscoveryRequest(org.junit.platform.engine.EngineDiscoveryRequest) TrackLogRecords(org.junit.jupiter.api.fixtures.TrackLogRecords) ValueSource(org.junit.jupiter.params.provider.ValueSource) EngineFilter.includeEngines(org.junit.platform.launcher.EngineFilter.includeEngines) CollectionUtils.getOnlyElement(org.junit.platform.commons.util.CollectionUtils.getOnlyElement) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) UniqueId(org.junit.platform.engine.UniqueId) Mockito.verify(org.mockito.Mockito.verify) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ExceptionUtils(org.junit.platform.commons.util.ExceptionUtils) ExecutionRequest(org.junit.platform.engine.ExecutionRequest) DiscoverySelectors.selectPackage(org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) DiscoverySelectors.selectUniqueId(org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId) UniqueId(org.junit.platform.engine.UniqueId) TestDescriptorStub(org.junit.platform.fakes.TestDescriptorStub) EngineDiscoveryRequest(org.junit.platform.engine.EngineDiscoveryRequest) TestEngineSpy(org.junit.platform.fakes.TestEngineSpy) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 54 with UniqueId

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");
}
Also used : DiscoverySelectors.selectUniqueId(org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId) UniqueId(org.junit.platform.engine.UniqueId) DemoHierarchicalTestEngine(org.junit.platform.engine.support.hierarchical.DemoHierarchicalTestEngine) TestEngine(org.junit.platform.engine.TestEngine) EngineDiscoveryRequest(org.junit.platform.engine.EngineDiscoveryRequest) TestEngineStub(org.junit.platform.fakes.TestEngineStub) JUnitException(org.junit.platform.commons.JUnitException) PreconditionViolationException(org.junit.platform.commons.PreconditionViolationException) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 55 with UniqueId

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
}
Also used : UniqueId(org.junit.platform.engine.UniqueId) DiscoverySelectors.selectUniqueId(org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId) MultipleTestsTestCase(org.junit.platform.suite.engine.testcases.MultipleTestsTestCase) Test(org.junit.jupiter.api.Test)

Aggregations

UniqueId (org.junit.platform.engine.UniqueId)131 Test (org.junit.jupiter.api.Test)95 DiscoverySelectors.selectUniqueId (org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId)83 DynamicTest (org.junit.jupiter.api.DynamicTest)64 TestDescriptor (org.junit.platform.engine.TestDescriptor)29 UniqueIdSelector (org.junit.platform.engine.discovery.UniqueIdSelector)25 ClassSelector (org.junit.platform.engine.discovery.ClassSelector)15 EngineDiscoveryRequest (org.junit.platform.engine.EngineDiscoveryRequest)14 List (java.util.List)9 JupiterTestDescriptor (org.junit.jupiter.engine.descriptor.JupiterTestDescriptor)8 TestTemplateInvocationTestDescriptor (org.junit.jupiter.engine.descriptor.TestTemplateInvocationTestDescriptor)8 TestPlan (org.junit.platform.launcher.TestPlan)8 JUnitException (org.junit.platform.commons.JUnitException)7 Method (java.lang.reflect.Method)6 ArrayList (java.util.ArrayList)6 Optional (java.util.Optional)6 DynamicDescendantFilter (org.junit.jupiter.engine.descriptor.DynamicDescendantFilter)6 TestEngine (org.junit.platform.engine.TestEngine)6 TrackLogRecords (org.junit.jupiter.engine.TrackLogRecords)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5