Search in sources :

Example 1 with TestEngineSpy

use of org.junit.platform.fakes.TestEngineSpy 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 2 with TestEngineSpy

use of org.junit.platform.fakes.TestEngineSpy in project junit5 by junit-team.

the class DefaultLauncherTests method reportsDynamicTestDescriptorsCorrectly.

@Test
void reportsDynamicTestDescriptorsCorrectly() {
    var engineId = UniqueId.forEngine(TestEngineSpy.ID);
    var containerAndTestId = engineId.append("c&t", "c&t");
    var dynamicTestId = containerAndTestId.append("test", "test");
    var engine = new TestEngineSpy() {

        @Override
        public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId uniqueId) {
            super.discover(discoveryRequest, uniqueId);
            var engineDescriptor = new TestDescriptorStub(uniqueId, uniqueId.toString());
            engineDescriptor.addChild(new TestDescriptorStub(containerAndTestId, "c&t") {

                @Override
                public Type getType() {
                    return Type.CONTAINER_AND_TEST;
                }
            });
            return engineDescriptor;
        }

        @Override
        public void execute(ExecutionRequest request) {
            super.execute(request);
            var listener = request.getEngineExecutionListener();
            listener.executionStarted(request.getRootTestDescriptor());
            var containerAndTest = getOnlyElement(request.getRootTestDescriptor().getChildren());
            listener.executionStarted(containerAndTest);
            var dynamicTest = new TestDescriptorStub(dynamicTestId, "test");
            dynamicTest.setParent(containerAndTest);
            listener.dynamicTestRegistered(dynamicTest);
            listener.executionStarted(dynamicTest);
            listener.executionFinished(dynamicTest, successful());
            listener.executionFinished(containerAndTest, successful());
            listener.executionFinished(request.getRootTestDescriptor(), successful());
        }
    };
    var launcher = createLauncher(engine);
    var listener = mock(TestExecutionListener.class);
    launcher.execute(request().build(), listener);
    var inOrder = inOrder(listener);
    var testPlanArgumentCaptor = ArgumentCaptor.forClass(TestPlan.class);
    inOrder.verify(listener).testPlanExecutionStarted(testPlanArgumentCaptor.capture());
    var testPlan = testPlanArgumentCaptor.getValue();
    var engineTestIdentifier = testPlan.getTestIdentifier(engineId.toString());
    var containerAndTestIdentifier = testPlan.getTestIdentifier(containerAndTestId.toString());
    var dynamicTestIdentifier = testPlan.getTestIdentifier(dynamicTestId.toString());
    assertThat(engineTestIdentifier.getParentId()).isEmpty();
    assertThat(containerAndTestIdentifier.getParentId()).contains(engineId.toString());
    assertThat(dynamicTestIdentifier.getParentId()).contains(containerAndTestId.toString());
    inOrder.verify(listener).executionStarted(engineTestIdentifier);
    inOrder.verify(listener).executionStarted(containerAndTestIdentifier);
    inOrder.verify(listener).dynamicTestRegistered(dynamicTestIdentifier);
    inOrder.verify(listener).executionStarted(dynamicTestIdentifier);
    inOrder.verify(listener).executionFinished(dynamicTestIdentifier, successful());
    inOrder.verify(listener).executionFinished(containerAndTestIdentifier, successful());
    inOrder.verify(listener).executionFinished(engineTestIdentifier, successful());
    inOrder.verify(listener).testPlanExecutionFinished(same(testPlan));
}
Also used : DiscoverySelectors.selectUniqueId(org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId) UniqueId(org.junit.platform.engine.UniqueId) ExecutionRequest(org.junit.platform.engine.ExecutionRequest) 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 3 with TestEngineSpy

use of org.junit.platform.fakes.TestEngineSpy in project junit5 by junit-team.

the class DefaultLauncherTests method testPlanThrowsExceptionWhenModified.

@Test
@SuppressWarnings("deprecation")
void testPlanThrowsExceptionWhenModified() {
    TestEngine engine = new TestEngineSpy();
    var launcher = createLauncher(engine);
    var testPlan = launcher.discover(request().build());
    var engineIdentifier = getOnlyElement(testPlan.getRoots());
    var engineUniqueId = UniqueId.parse(engineIdentifier.getUniqueId());
    assertThat(testPlan.getChildren(engineIdentifier)).hasSize(1);
    var addedIdentifier = TestIdentifier.from(new TestDescriptorStub(engineUniqueId.append("test", "test2"), "test2"));
    var exception = assertThrows(JUnitException.class, () -> testPlan.add(addedIdentifier));
    assertThat(exception).hasMessage("Unsupported attempt to modify the TestPlan was detected. " + "Please contact your IDE/tool vendor and request a fix or downgrade to JUnit 5.7.x (see https://github.com/junit-team/junit5/issues/1732 for details).");
    assertThat(testPlan.getChildren(engineIdentifier)).hasSize(1).doesNotContain(addedIdentifier);
}
Also used : DemoHierarchicalTestEngine(org.junit.platform.engine.support.hierarchical.DemoHierarchicalTestEngine) TestEngine(org.junit.platform.engine.TestEngine) TestDescriptorStub(org.junit.platform.fakes.TestDescriptorStub) TestEngineSpy(org.junit.platform.fakes.TestEngineSpy) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Test (org.junit.jupiter.api.Test)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 TestDescriptorStub (org.junit.platform.fakes.TestDescriptorStub)3 TestEngineSpy (org.junit.platform.fakes.TestEngineSpy)3 EngineDiscoveryRequest (org.junit.platform.engine.EngineDiscoveryRequest)2 ExecutionRequest (org.junit.platform.engine.ExecutionRequest)2 TestEngine (org.junit.platform.engine.TestEngine)2 UniqueId (org.junit.platform.engine.UniqueId)2 DiscoverySelectors.selectUniqueId (org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId)2 DemoHierarchicalTestEngine (org.junit.platform.engine.support.hierarchical.DemoHierarchicalTestEngine)2 Level (java.util.logging.Level)1 LogRecord (java.util.logging.LogRecord)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1 TrackLogRecords (org.junit.jupiter.api.fixtures.TrackLogRecords)1 ValueSource (org.junit.jupiter.params.provider.ValueSource)1 JUnitException (org.junit.platform.commons.JUnitException)1 PreconditionViolationException (org.junit.platform.commons.PreconditionViolationException)1