use of org.junit.jupiter.api.extension.TestInstances in project junit5 by junit-team.
the class ClassBasedTestDescriptor method before.
@Override
public JupiterEngineExecutionContext before(JupiterEngineExecutionContext context) {
ThrowableCollector throwableCollector = context.getThrowableCollector();
if (isPerClassLifecycle(context)) {
// Eagerly load test instance for BeforeAllCallbacks, if necessary,
// and store the instance in the ExtensionContext.
ClassExtensionContext extensionContext = (ClassExtensionContext) context.getExtensionContext();
throwableCollector.execute(() -> {
TestInstances testInstances = context.getTestInstancesProvider().getTestInstances(context.getExtensionRegistry(), throwableCollector);
extensionContext.setTestInstances(testInstances);
});
}
if (throwableCollector.isEmpty()) {
context.beforeAllCallbacksExecuted(true);
invokeBeforeAllCallbacks(context);
if (throwableCollector.isEmpty()) {
context.beforeAllMethodsExecuted(true);
invokeBeforeAllMethods(context);
}
}
throwableCollector.assertEmpty();
return context;
}
use of org.junit.jupiter.api.extension.TestInstances in project junit5 by junit-team.
the class TestInstanceLifecycleTests method instancePerMethodOnOuterTestClassWithInstancePerClassOnNestedTestClass.
@Test
void instancePerMethodOnOuterTestClassWithInstancePerClassOnNestedTestClass() {
Class<?> testClass = MixedLifecyclesOuterTestCase.class;
Class<?> nestedTestClass = MixedLifecyclesOuterTestCase.NestedInstancePerClassTestCase.class;
int containers = 4;
int tests = 4;
Map.Entry<Class<?>, Integer>[] instances = instanceCounts(entry(testClass, 2), entry(nestedTestClass, 1));
int allMethods = 1;
int eachMethods = 7;
performAssertions(testClass, containers, tests, instances, allMethods, eachMethods);
String containerExecutionConditionKey = executionConditionKey(testClass, null);
String nestedContainerExecutionConditionKey = executionConditionKey(nestedTestClass, null);
String nestedTestTemplateKey = testTemplateKey(nestedTestClass, "singletonTest");
String postProcessTestInstanceKey = postProcessTestInstanceKey(testClass);
String nestedPostProcessTestInstanceKey = postProcessTestInstanceKey(nestedTestClass);
String preDestroyCallbackTestInstanceKey = preDestroyCallbackTestInstanceKey(testClass);
String nestedPreDestroyCallbackTestInstanceKey = preDestroyCallbackTestInstanceKey(nestedTestClass);
String beforeAllCallbackKey = beforeAllCallbackKey(testClass);
String afterAllCallbackKey = afterAllCallbackKey(testClass);
String outerTestExecutionConditionKey = executionConditionKey(testClass, "outerTest");
String beforeEachCallbackKey = beforeEachCallbackKey(testClass, "outerTest");
String afterEachCallbackKey = afterEachCallbackKey(testClass, "outerTest");
String nestedBeforeAllCallbackKey = beforeAllCallbackKey(nestedTestClass);
String nestedAfterAllCallbackKey = afterAllCallbackKey(nestedTestClass);
String nestedExecutionConditionKey1 = executionConditionKey(nestedTestClass, testsInvoked.get(0));
String nestedBeforeEachCallbackKey1 = beforeEachCallbackKey(nestedTestClass, testsInvoked.get(0));
String nestedAfterEachCallbackKey1 = afterEachCallbackKey(nestedTestClass, testsInvoked.get(0));
String nestedExecutionConditionKey2 = executionConditionKey(nestedTestClass, testsInvoked.get(1));
String nestedBeforeEachCallbackKey2 = beforeEachCallbackKey(nestedTestClass, testsInvoked.get(1));
String nestedAfterEachCallbackKey2 = afterEachCallbackKey(nestedTestClass, testsInvoked.get(1));
String nestedExecutionConditionKey3 = executionConditionKey(nestedTestClass, testsInvoked.get(2));
String nestedBeforeEachCallbackKey3 = beforeEachCallbackKey(nestedTestClass, testsInvoked.get(2));
String nestedAfterEachCallbackKey3 = afterEachCallbackKey(nestedTestClass, testsInvoked.get(2));
// @formatter:off
assertThat(instanceMap.keySet()).containsExactlyInAnyOrder(containerExecutionConditionKey, nestedTestTemplateKey, nestedContainerExecutionConditionKey, postProcessTestInstanceKey, nestedPostProcessTestInstanceKey, preDestroyCallbackTestInstanceKey, nestedPreDestroyCallbackTestInstanceKey, beforeAllCallbackKey, afterAllCallbackKey, outerTestExecutionConditionKey, beforeEachCallbackKey, afterEachCallbackKey, nestedBeforeAllCallbackKey, nestedAfterAllCallbackKey, nestedExecutionConditionKey1, nestedBeforeEachCallbackKey1, nestedAfterEachCallbackKey1, nestedExecutionConditionKey2, nestedBeforeEachCallbackKey2, nestedAfterEachCallbackKey2, nestedExecutionConditionKey3, nestedBeforeEachCallbackKey3, nestedAfterEachCallbackKey3);
// @formatter:on
assertNull(instanceMap.get(containerExecutionConditionKey));
assertNull(instanceMap.get(beforeAllCallbackKey));
assertNull(instanceMap.get(afterAllCallbackKey));
TestInstances outerInstances = instanceMap.get(beforeEachCallbackKey);
assertSame(outerInstances, instanceMap.get(afterEachCallbackKey));
assertSame(outerInstances, instanceMap.get(outerTestExecutionConditionKey));
Object nestedInstance = instanceMap.get(nestedPostProcessTestInstanceKey).getInnermostInstance();
assertNotNull(nestedInstance);
assertNotSame(outerInstances.getInnermostInstance(), nestedInstance);
assertNull(instanceMap.get(nestedContainerExecutionConditionKey));
assertSame(nestedInstance, instanceMap.get(nestedBeforeAllCallbackKey).getInnermostInstance());
assertSame(nestedInstance, instanceMap.get(nestedAfterAllCallbackKey).getInnermostInstance());
assertSame(nestedInstance, instanceMap.get(nestedExecutionConditionKey1).getInnermostInstance());
assertSame(nestedInstance, instanceMap.get(nestedBeforeEachCallbackKey1).getInnermostInstance());
assertSame(nestedInstance, instanceMap.get(nestedAfterEachCallbackKey1).getInnermostInstance());
assertSame(nestedInstance, instanceMap.get(nestedExecutionConditionKey2).getInnermostInstance());
assertSame(nestedInstance, instanceMap.get(nestedBeforeEachCallbackKey2).getInnermostInstance());
assertSame(nestedInstance, instanceMap.get(nestedAfterEachCallbackKey2).getInnermostInstance());
assertSame(nestedInstance, instanceMap.get(nestedExecutionConditionKey3).getInnermostInstance());
assertSame(nestedInstance, instanceMap.get(nestedBeforeEachCallbackKey3).getInnermostInstance());
assertSame(nestedInstance, instanceMap.get(nestedAfterEachCallbackKey3).getInnermostInstance());
assertSame(nestedInstance, instanceMap.get(nestedPreDestroyCallbackTestInstanceKey).getInnermostInstance());
// The last tracked instance stored under postProcessTestInstanceKey
// is only created in order to instantiate the nested test class.
Object outerInstance = instanceMap.get(nestedExecutionConditionKey1).findInstance(testClass).get();
assertEquals(outerInstances.getInnermostInstance().getClass(), outerInstance.getClass());
assertNotSame(outerInstances.getInnermostInstance(), outerInstance);
assertThat(instanceMap.get(nestedExecutionConditionKey1).getAllInstances()).containsExactly(outerInstance, nestedInstance);
assertThat(instanceMap.get(nestedBeforeEachCallbackKey1).getAllInstances()).containsExactly(outerInstance, nestedInstance);
assertThat(instanceMap.get(nestedAfterEachCallbackKey1).getAllInstances()).containsExactly(outerInstance, nestedInstance);
assertThat(instanceMap.get(nestedExecutionConditionKey2).getAllInstances()).containsExactly(outerInstance, nestedInstance);
assertThat(instanceMap.get(nestedBeforeEachCallbackKey2).getAllInstances()).containsExactly(outerInstance, nestedInstance);
assertThat(instanceMap.get(nestedAfterEachCallbackKey2).getAllInstances()).containsExactly(outerInstance, nestedInstance);
assertThat(instanceMap.get(nestedExecutionConditionKey3).getAllInstances()).containsExactly(outerInstance, nestedInstance);
assertThat(instanceMap.get(nestedBeforeEachCallbackKey3).getAllInstances()).containsExactly(outerInstance, nestedInstance);
assertThat(instanceMap.get(nestedAfterEachCallbackKey3).getAllInstances()).containsExactly(outerInstance, nestedInstance);
assertThat(lifecyclesMap.keySet()).containsExactly(testClass, nestedTestClass);
assertThat(lifecyclesMap.get(testClass).stream()).allMatch(Lifecycle.PER_METHOD::equals);
assertThat(lifecyclesMap.get(nestedTestClass).stream()).allMatch(Lifecycle.PER_CLASS::equals);
}
use of org.junit.jupiter.api.extension.TestInstances in project junit5 by junit-team.
the class TestInstanceLifecycleTests method instancePerMethodWithNestedTestClass.
@Test
void instancePerMethodWithNestedTestClass() {
Class<?> testClass = InstancePerMethodOuterTestCase.class;
Class<?> nestedTestClass = InstancePerMethodOuterTestCase.NestedInstancePerMethodTestCase.class;
int containers = 4;
int tests = 4;
Map.Entry<Class<?>, Integer>[] instances = instanceCounts(entry(testClass, 4), entry(nestedTestClass, 3));
int allMethods = 1;
int eachMethods = 3;
performAssertions(testClass, containers, tests, instances, allMethods, eachMethods);
String containerExecutionConditionKey = executionConditionKey(testClass, null);
String nestedContainerExecutionConditionKey = executionConditionKey(nestedTestClass, null);
String nestedTestTemplateKey = testTemplateKey(nestedTestClass, "singletonTest");
String postProcessTestInstanceKey = postProcessTestInstanceKey(testClass);
String nestedPostProcessTestInstanceKey = postProcessTestInstanceKey(nestedTestClass);
String preDestroyCallbackTestInstanceKey = preDestroyCallbackTestInstanceKey(testClass);
String nestedPreDestroyCallbackTestInstanceKey = preDestroyCallbackTestInstanceKey(nestedTestClass);
String beforeAllCallbackKey = beforeAllCallbackKey(testClass);
String afterAllCallbackKey = afterAllCallbackKey(testClass);
String outerTestExecutionConditionKey = executionConditionKey(testClass, "outerTest");
String beforeEachCallbackKey = beforeEachCallbackKey(testClass, "outerTest");
String afterEachCallbackKey = afterEachCallbackKey(testClass, "outerTest");
String nestedBeforeAllCallbackKey = beforeAllCallbackKey(nestedTestClass);
String nestedAfterAllCallbackKey = afterAllCallbackKey(nestedTestClass);
String nestedExecutionConditionKey1 = executionConditionKey(nestedTestClass, testsInvoked.get(0));
String nestedBeforeEachCallbackKey1 = beforeEachCallbackKey(nestedTestClass, testsInvoked.get(0));
String nestedAfterEachCallbackKey1 = afterEachCallbackKey(nestedTestClass, testsInvoked.get(0));
String nestedExecutionConditionKey2 = executionConditionKey(nestedTestClass, testsInvoked.get(1));
String nestedBeforeEachCallbackKey2 = beforeEachCallbackKey(nestedTestClass, testsInvoked.get(1));
String nestedAfterEachCallbackKey2 = afterEachCallbackKey(nestedTestClass, testsInvoked.get(1));
String nestedExecutionConditionKey3 = executionConditionKey(nestedTestClass, testsInvoked.get(2));
String nestedBeforeEachCallbackKey3 = beforeEachCallbackKey(nestedTestClass, testsInvoked.get(2));
String nestedAfterEachCallbackKey3 = afterEachCallbackKey(nestedTestClass, testsInvoked.get(2));
// @formatter:off
assertThat(instanceMap.keySet()).containsExactlyInAnyOrder(containerExecutionConditionKey, nestedTestTemplateKey, nestedContainerExecutionConditionKey, postProcessTestInstanceKey, nestedPostProcessTestInstanceKey, preDestroyCallbackTestInstanceKey, nestedPreDestroyCallbackTestInstanceKey, beforeAllCallbackKey, afterAllCallbackKey, outerTestExecutionConditionKey, beforeEachCallbackKey, afterEachCallbackKey, nestedBeforeAllCallbackKey, nestedAfterAllCallbackKey, nestedExecutionConditionKey1, nestedBeforeEachCallbackKey1, nestedAfterEachCallbackKey1, nestedExecutionConditionKey2, nestedBeforeEachCallbackKey2, nestedAfterEachCallbackKey2, nestedExecutionConditionKey3, nestedBeforeEachCallbackKey3, nestedAfterEachCallbackKey3);
// @formatter:on
assertNull(instanceMap.get(containerExecutionConditionKey));
assertNull(instanceMap.get(beforeAllCallbackKey));
assertNull(instanceMap.get(afterAllCallbackKey));
assertNull(instanceMap.get(nestedContainerExecutionConditionKey));
assertNull(instanceMap.get(nestedBeforeAllCallbackKey));
assertNull(instanceMap.get(nestedAfterAllCallbackKey));
TestInstances outerInstances = instanceMap.get(beforeEachCallbackKey);
assertNotNull(outerInstances.getInnermostInstance());
assertSame(outerInstances, instanceMap.get(afterEachCallbackKey));
assertSame(outerInstances, instanceMap.get(outerTestExecutionConditionKey));
TestInstances nestedInstances1 = instanceMap.get(nestedBeforeEachCallbackKey1);
assertNotNull(nestedInstances1.getInnermostInstance());
assertNotSame(outerInstances.getInnermostInstance(), nestedInstances1.getInnermostInstance());
assertSame(nestedInstances1, instanceMap.get(nestedAfterEachCallbackKey1));
assertSame(nestedInstances1, instanceMap.get(nestedExecutionConditionKey1));
TestInstances nestedInstances2 = instanceMap.get(nestedBeforeEachCallbackKey2);
assertNotNull(nestedInstances2.getInnermostInstance());
assertNotSame(outerInstances.getInnermostInstance(), nestedInstances2.getInnermostInstance());
assertNotSame(nestedInstances1.getInnermostInstance(), nestedInstances2.getInnermostInstance());
assertSame(nestedInstances2, instanceMap.get(nestedAfterEachCallbackKey2));
assertSame(nestedInstances2, instanceMap.get(nestedExecutionConditionKey2));
TestInstances nestedInstances3 = instanceMap.get(nestedPostProcessTestInstanceKey);
assertNotNull(nestedInstances3.getInnermostInstance());
assertNotSame(outerInstances.getInnermostInstance(), nestedInstances3.getInnermostInstance());
assertNotSame(nestedInstances1.getInnermostInstance(), nestedInstances3.getInnermostInstance());
assertSame(nestedInstances3.getInnermostInstance(), instanceMap.get(nestedAfterEachCallbackKey3).getInnermostInstance());
assertSame(nestedInstances3.getInnermostInstance(), instanceMap.get(nestedExecutionConditionKey3).getInnermostInstance());
assertSame(nestedInstances3.getInnermostInstance(), instanceMap.get(nestedPreDestroyCallbackTestInstanceKey).getInnermostInstance());
Object outerInstance1 = instanceMap.get(nestedExecutionConditionKey1).findInstance(testClass).get();
Object outerInstance2 = instanceMap.get(nestedExecutionConditionKey2).findInstance(testClass).get();
Object outerInstance3 = instanceMap.get(nestedExecutionConditionKey3).findInstance(testClass).get();
assertNotSame(outerInstance1, outerInstance2);
assertNotSame(outerInstance1, outerInstance3);
assertThat(instanceMap.get(nestedExecutionConditionKey1).getAllInstances()).containsExactly(outerInstance1, nestedInstances1.getInnermostInstance());
assertThat(instanceMap.get(nestedExecutionConditionKey2).getAllInstances()).containsExactly(outerInstance2, nestedInstances2.getInnermostInstance());
assertThat(instanceMap.get(nestedExecutionConditionKey3).getAllInstances()).containsExactly(outerInstance3, nestedInstances3.getInnermostInstance());
// The last tracked instance stored under postProcessTestInstanceKey
// is only created in order to instantiate the nested test class for
// test2().
assertSame(outerInstance3, instanceMap.get(postProcessTestInstanceKey).getInnermostInstance());
assertThat(lifecyclesMap.keySet()).containsExactly(testClass, nestedTestClass);
assertThat(lifecyclesMap.get(testClass).stream()).allMatch(Lifecycle.PER_METHOD::equals);
assertThat(lifecyclesMap.get(nestedTestClass).stream()).allMatch(Lifecycle.PER_METHOD::equals);
}
use of org.junit.jupiter.api.extension.TestInstances in project neo4j by neo4j.
the class Neo4jLayoutSupportExtension method prepare.
private void prepare(ExtensionContext context) {
TestInstances testInstances = context.getRequiredTestInstances();
TestDirectory testDir = getTestDirectory(context);
Config config = Config.defaults(neo4j_home, testDir.homePath());
Neo4jLayout neo4jLayout = Neo4jLayout.of(config);
DatabaseLayout databaseLayout = neo4jLayout.databaseLayout(config.get(default_database));
createDirectories(testDir.getFileSystem(), neo4jLayout, databaseLayout);
for (Object testInstance : testInstances.getAllInstances()) {
injectInstance(testInstance, neo4jLayout, Neo4jLayout.class);
injectInstance(testInstance, databaseLayout, DatabaseLayout.class);
}
}
use of org.junit.jupiter.api.extension.TestInstances in project junit5 by junit-team.
the class TestInstanceLifecycleTests method instancePerClass.
private void instancePerClass(Class<?> testClass, Map.Entry<Class<?>, Integer>[] instances) {
int containers = 3;
int tests = 3;
int allMethods = 2;
int eachMethods = 3;
performAssertions(testClass, containers, tests, instances, allMethods, eachMethods);
String containerExecutionConditionKey = executionConditionKey(testClass, null);
String testTemplateKey = testTemplateKey(testClass, "singletonTest");
String postProcessTestInstanceKey = postProcessTestInstanceKey(testClass);
String preDestroyCallbackTestInstanceKey = preDestroyCallbackTestInstanceKey(testClass);
String beforeAllCallbackKey = beforeAllCallbackKey(testClass);
String afterAllCallbackKey = afterAllCallbackKey(testClass);
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(postProcessTestInstanceKey, preDestroyCallbackTestInstanceKey, containerExecutionConditionKey, beforeAllCallbackKey, testTemplateKey, testExecutionConditionKey1, beforeEachCallbackKey1, afterEachCallbackKey1, testExecutionConditionKey2, beforeEachCallbackKey2, afterEachCallbackKey2, testExecutionConditionKey3, beforeEachCallbackKey3, afterEachCallbackKey3, afterAllCallbackKey);
// @formatter:on
TestInstances testInstances = instanceMap.get(beforeAllCallbackKey);
assertNotNull(testInstances.getInnermostInstance());
assertSame(testInstances, instanceMap.get(afterAllCallbackKey));
assertSame(testInstances, instanceMap.get(testExecutionConditionKey1));
assertSame(testInstances, instanceMap.get(beforeEachCallbackKey1));
assertSame(testInstances, instanceMap.get(afterEachCallbackKey1));
assertSame(testInstances, instanceMap.get(testExecutionConditionKey2));
assertSame(testInstances, instanceMap.get(beforeEachCallbackKey2));
assertSame(testInstances, instanceMap.get(afterEachCallbackKey2));
assertSame(testInstances, instanceMap.get(testExecutionConditionKey3));
assertSame(testInstances, instanceMap.get(beforeEachCallbackKey3));
assertSame(testInstances, instanceMap.get(afterEachCallbackKey3));
assertSame(testInstances.getInnermostInstance(), instanceMap.get(postProcessTestInstanceKey).getInnermostInstance());
assertSame(testInstances.getInnermostInstance(), instanceMap.get(preDestroyCallbackTestInstanceKey).getInnermostInstance());
assertNull(instanceMap.get(containerExecutionConditionKey));
assertThat(lifecyclesMap.keySet()).containsExactly(testClass);
assertThat(lifecyclesMap.get(testClass).stream()).allMatch(Lifecycle.PER_CLASS::equals);
}
Aggregations