Search in sources :

Example 11 with TestInstances

use of org.junit.jupiter.api.extension.TestInstances in project junit5 by junit-team.

the class TestInstanceLifecycleTests method instancePerMethod.

@Test
void instancePerMethod() {
    Class<?> testClass = InstancePerMethodTestCase.class;
    int containers = 3;
    int tests = 3;
    Map.Entry<Class<?>, Integer>[] instances = instanceCounts(entry(InstancePerMethodTestCase.class, 3));
    int allMethods = 1;
    int eachMethods = 3;
    performAssertions(testClass, containers, tests, instances, allMethods, eachMethods);
    String containerExecutionConditionKey = executionConditionKey(testClass, null);
    String postProcessTestInstanceKey = postProcessTestInstanceKey(testClass);
    String preDestroyCallbackTestInstanceKey = preDestroyCallbackTestInstanceKey(testClass);
    String beforeAllCallbackKey = beforeAllCallbackKey(testClass);
    String afterAllCallbackKey = afterAllCallbackKey(testClass);
    String testTemplateKey = testTemplateKey(testClass, "singletonTest");
    String testExecutionConditionKey1 = executionConditionKey(testClass, testsInvoked.get(0));
    String beforeEachCallbackKey1 = beforeEachCallbackKey(testClass, testsInvoked.get(0));
    String afterEachCallbackKey1 = afterEachCallbackKey(testClass, testsInvoked.get(0));
    String testExecutionConditionKey2 = executionConditionKey(testClass, testsInvoked.get(1));
    String beforeEachCallbackKey2 = beforeEachCallbackKey(testClass, testsInvoked.get(1));
    String afterEachCallbackKey2 = afterEachCallbackKey(testClass, testsInvoked.get(1));
    String testExecutionConditionKey3 = executionConditionKey(testClass, testsInvoked.get(2));
    String beforeEachCallbackKey3 = beforeEachCallbackKey(testClass, testsInvoked.get(2));
    String afterEachCallbackKey3 = afterEachCallbackKey(testClass, testsInvoked.get(2));
    // @formatter:off
    assertThat(instanceMap.keySet()).containsExactlyInAnyOrder(containerExecutionConditionKey, beforeAllCallbackKey, postProcessTestInstanceKey, preDestroyCallbackTestInstanceKey, testTemplateKey, testExecutionConditionKey1, beforeEachCallbackKey1, afterEachCallbackKey1, testExecutionConditionKey2, beforeEachCallbackKey2, afterEachCallbackKey2, testExecutionConditionKey3, beforeEachCallbackKey3, afterEachCallbackKey3, afterAllCallbackKey);
    // @formatter:on
    assertNull(instanceMap.get(containerExecutionConditionKey));
    assertNull(instanceMap.get(beforeAllCallbackKey));
    assertNull(instanceMap.get(afterAllCallbackKey));
    TestInstances testInstances = instanceMap.get(beforeEachCallbackKey1);
    assertNotNull(testInstances.getInnermostInstance());
    assertSame(testInstances, instanceMap.get(afterEachCallbackKey1));
    assertSame(testInstances, instanceMap.get(testExecutionConditionKey1));
    testInstances = instanceMap.get(beforeEachCallbackKey2);
    assertNotNull(testInstances.getInnermostInstance());
    assertSame(testInstances, instanceMap.get(afterEachCallbackKey2));
    assertSame(testInstances, instanceMap.get(testExecutionConditionKey2));
    testInstances = instanceMap.get(beforeEachCallbackKey3);
    assertNotNull(testInstances.getInnermostInstance());
    assertSame(testInstances, instanceMap.get(afterEachCallbackKey3));
    assertSame(testInstances, instanceMap.get(testExecutionConditionKey3));
    assertSame(testInstances.getInnermostInstance(), instanceMap.get(postProcessTestInstanceKey).getInnermostInstance());
    assertSame(testInstances.getInnermostInstance(), instanceMap.get(preDestroyCallbackTestInstanceKey).getInnermostInstance());
    assertThat(lifecyclesMap.keySet()).containsExactly(testClass);
    assertThat(lifecyclesMap.get(testClass).stream()).allMatch(Lifecycle.PER_METHOD::equals);
}
Also used : TestInstances(org.junit.jupiter.api.extension.TestInstances) DefaultTestInstances(org.junit.jupiter.engine.execution.DefaultTestInstances) Test(org.junit.jupiter.api.Test)

Example 12 with TestInstances

use of org.junit.jupiter.api.extension.TestInstances in project junit5 by junit-team.

the class NestedClassTestDescriptor method instantiateTestClass.

// --- Node ----------------------------------------------------------------
@Override
protected TestInstances instantiateTestClass(JupiterEngineExecutionContext parentExecutionContext, ExtensionRegistry registry, ExtensionRegistrar registrar, ExtensionContext extensionContext, ThrowableCollector throwableCollector) {
    // Extensions registered for nested classes and below are not to be used for instantiating and initializing outer classes
    ExtensionRegistry extensionRegistryForOuterInstanceCreation = parentExecutionContext.getExtensionRegistry();
    TestInstances outerInstances = parentExecutionContext.getTestInstancesProvider().getTestInstances(extensionRegistryForOuterInstanceCreation, registrar, throwableCollector);
    return instantiateTestClass(Optional.of(outerInstances), registry, extensionContext);
}
Also used : TestInstances(org.junit.jupiter.api.extension.TestInstances) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry)

Example 13 with TestInstances

use of org.junit.jupiter.api.extension.TestInstances in project junit5 by junit-team.

the class TestMethodTestDescriptor method prepare.

// --- Node ----------------------------------------------------------------
@Override
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) {
    MutableExtensionRegistry registry = populateNewExtensionRegistry(context);
    ThrowableCollector throwableCollector = createThrowableCollector();
    ExecutableInvoker executableInvoker = new DefaultExecutableInvoker(context);
    MethodExtensionContext extensionContext = new MethodExtensionContext(context.getExtensionContext(), context.getExecutionListener(), this, context.getConfiguration(), throwableCollector, executableInvoker);
    throwableCollector.execute(() -> {
        TestInstances testInstances = context.getTestInstancesProvider().getTestInstances(registry, throwableCollector);
        extensionContext.setTestInstances(testInstances);
    });
    // @formatter:off
    return context.extend().withExtensionRegistry(registry).withExtensionContext(extensionContext).withThrowableCollector(throwableCollector).build();
// @formatter:on
}
Also used : MutableExtensionRegistry(org.junit.jupiter.engine.extension.MutableExtensionRegistry) ExecutableInvoker(org.junit.jupiter.api.extension.ExecutableInvoker) DefaultExecutableInvoker(org.junit.jupiter.engine.execution.DefaultExecutableInvoker) InterceptingExecutableInvoker(org.junit.jupiter.engine.execution.InterceptingExecutableInvoker) TestInstances(org.junit.jupiter.api.extension.TestInstances) DefaultExecutableInvoker(org.junit.jupiter.engine.execution.DefaultExecutableInvoker) ThrowableCollector(org.junit.platform.engine.support.hierarchical.ThrowableCollector) JupiterThrowableCollectorFactory.createThrowableCollector(org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory.createThrowableCollector)

Example 14 with TestInstances

use of org.junit.jupiter.api.extension.TestInstances in project junit5 by junit-team.

the class TestTemplateTestDescriptor method prepare.

// --- Node ----------------------------------------------------------------
@Override
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) throws Exception {
    MutableExtensionRegistry registry = populateNewExtensionRegistryFromExtendWithAnnotation(context.getExtensionRegistry(), getTestMethod());
    // The test instance should be properly maintained by the enclosing class's ExtensionContext.
    TestInstances testInstances = context.getExtensionContext().getTestInstances().orElse(null);
    ExecutableInvoker executableInvoker = new DefaultExecutableInvoker(context);
    ExtensionContext extensionContext = new TestTemplateExtensionContext(context.getExtensionContext(), context.getExecutionListener(), this, context.getConfiguration(), testInstances, executableInvoker);
    // @formatter:off
    return context.extend().withExtensionRegistry(registry).withExtensionContext(extensionContext).build();
// @formatter:on
}
Also used : MutableExtensionRegistry(org.junit.jupiter.engine.extension.MutableExtensionRegistry) DefaultExecutableInvoker(org.junit.jupiter.engine.execution.DefaultExecutableInvoker) ExecutableInvoker(org.junit.jupiter.api.extension.ExecutableInvoker) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) TestInstances(org.junit.jupiter.api.extension.TestInstances) DefaultExecutableInvoker(org.junit.jupiter.engine.execution.DefaultExecutableInvoker)

Aggregations

TestInstances (org.junit.jupiter.api.extension.TestInstances)14 DefaultTestInstances (org.junit.jupiter.engine.execution.DefaultTestInstances)7 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)3 Test (org.junit.jupiter.api.Test)3 ExecutableInvoker (org.junit.jupiter.api.extension.ExecutableInvoker)2 DefaultExecutableInvoker (org.junit.jupiter.engine.execution.DefaultExecutableInvoker)2 MutableExtensionRegistry (org.junit.jupiter.engine.extension.MutableExtensionRegistry)2 JupiterThrowableCollectorFactory.createThrowableCollector (org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory.createThrowableCollector)2 ThrowableCollector (org.junit.platform.engine.support.hierarchical.ThrowableCollector)2 ApplicationComposers (org.apache.openejb.testing.ApplicationComposers)1 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)1 InterceptingExecutableInvoker (org.junit.jupiter.engine.execution.InterceptingExecutableInvoker)1 ExtensionRegistry (org.junit.jupiter.engine.extension.ExtensionRegistry)1 JUnitException (org.junit.platform.commons.JUnitException)1 Config (org.neo4j.configuration.Config)1 DatabaseLayout (org.neo4j.io.layout.DatabaseLayout)1 Neo4jLayout (org.neo4j.io.layout.Neo4jLayout)1 TestDirectory (org.neo4j.test.rule.TestDirectory)1