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);
}
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);
}
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
}
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
}
Aggregations