use of org.junit.jupiter.engine.descriptor.ClassExtensionContext in project junit5 by junit-team.
the class ExtensionContextTests method fromClassTestDescriptor.
@Test
@SuppressWarnings("resource")
void fromClassTestDescriptor() {
ClassTestDescriptor nestedClassDescriptor = nestedClassDescriptor();
ClassTestDescriptor outerClassDescriptor = outerClassDescriptor(nestedClassDescriptor);
ClassExtensionContext outerExtensionContext = new ClassExtensionContext(null, null, outerClassDescriptor, configParams, null);
// @formatter:off
assertAll("outerContext", () -> assertThat(outerExtensionContext.getElement()).contains(OuterClass.class), () -> assertThat(outerExtensionContext.getTestClass()).contains(OuterClass.class), () -> assertThat(outerExtensionContext.getTestInstance()).isEmpty(), () -> assertThat(outerExtensionContext.getTestMethod()).isEmpty(), () -> assertThat(outerExtensionContext.getRequiredTestClass()).isEqualTo(OuterClass.class), () -> assertThrows(PreconditionViolationException.class, () -> outerExtensionContext.getRequiredTestInstance()), () -> assertThrows(PreconditionViolationException.class, () -> outerExtensionContext.getRequiredTestMethod()), () -> assertThat(outerExtensionContext.getDisplayName()).isEqualTo(outerClassDescriptor.getDisplayName()), () -> assertThat(outerExtensionContext.getParent()).isEmpty());
// @formatter:on
ClassExtensionContext nestedExtensionContext = new ClassExtensionContext(outerExtensionContext, null, nestedClassDescriptor, configParams, null);
assertThat(nestedExtensionContext.getParent()).containsSame(outerExtensionContext);
}
use of org.junit.jupiter.engine.descriptor.ClassExtensionContext in project junit5 by junit-team.
the class ExtensionContextTests method configurationParameter.
@TestFactory
Stream<DynamicTest> configurationParameter() throws Exception {
ConfigurationParameters echo = new EchoParameters();
String key = "123";
Optional<String> expected = Optional.of(key);
UniqueId engineUniqueId = UniqueId.parse("[engine:junit-jupiter]");
JupiterEngineDescriptor engineDescriptor = new JupiterEngineDescriptor(engineUniqueId);
UniqueId classUniqueId = UniqueId.parse("[engine:junit-jupiter]/[class:MyClass]");
ClassTestDescriptor classTestDescriptor = new ClassTestDescriptor(classUniqueId, getClass());
Method method = getClass().getDeclaredMethod("configurationParameter");
UniqueId methodUniqueId = UniqueId.parse("[engine:junit-jupiter]/[class:MyClass]/[method:myMethod]");
TestMethodTestDescriptor methodTestDescriptor = new TestMethodTestDescriptor(methodUniqueId, getClass(), method);
return //
Stream.of(//
(ExtensionContext) new JupiterEngineExtensionContext(null, engineDescriptor, echo), //
new ClassExtensionContext(null, null, classTestDescriptor, echo, null), //
new MethodExtensionContext(null, null, methodTestDescriptor, echo, null, null)).map(context -> dynamicTest(context.getClass().getSimpleName(), () -> assertEquals(expected, context.getConfigurationParameter(key))));
}
use of org.junit.jupiter.engine.descriptor.ClassExtensionContext in project junit5 by junit-team.
the class JupiterEngineExecutionContextTests method canOverrideAttributeWhenContextIsExtended.
@Test
void canOverrideAttributeWhenContextIsExtended() {
UniqueId uniqueId = UniqueId.parse("[engine:junit-jupiter]/[class:MyClass]");
ClassTestDescriptor classTestDescriptor = new ClassTestDescriptor(uniqueId, getClass());
ClassExtensionContext extensionContext = new ClassExtensionContext(null, null, classTestDescriptor, configParams, null);
ExtensionRegistry extensionRegistry = ExtensionRegistry.createRegistryWithDefaultExtensions(configParams);
TestInstanceProvider testInstanceProvider = mock(TestInstanceProvider.class);
ClassExtensionContext newExtensionContext = new ClassExtensionContext(extensionContext, null, classTestDescriptor, configParams, null);
JupiterEngineExecutionContext newContext = //
originalContext.extend().withExtensionContext(//
extensionContext).withExtensionRegistry(//
extensionRegistry).withTestInstanceProvider(//
testInstanceProvider).build().extend().withExtensionContext(//
newExtensionContext).build();
assertSame(newExtensionContext, newContext.getExtensionContext());
assertSame(extensionRegistry, newContext.getExtensionRegistry());
assertSame(testInstanceProvider, newContext.getTestInstanceProvider());
}
use of org.junit.jupiter.engine.descriptor.ClassExtensionContext in project junit5 by junit-team.
the class ExtensionContextTests method fromMethodTestDescriptor.
@Test
@SuppressWarnings("resource")
void fromMethodTestDescriptor() {
TestMethodTestDescriptor methodTestDescriptor = methodDescriptor();
ClassTestDescriptor classTestDescriptor = outerClassDescriptor(methodTestDescriptor);
JupiterEngineDescriptor engineDescriptor = new JupiterEngineDescriptor(UniqueId.forEngine("junit-jupiter"));
engineDescriptor.addChild(classTestDescriptor);
Object testInstance = new OuterClass();
Method testMethod = methodTestDescriptor.getTestMethod();
JupiterEngineExtensionContext engineExtensionContext = new JupiterEngineExtensionContext(null, engineDescriptor, configParams);
ClassExtensionContext classExtensionContext = new ClassExtensionContext(engineExtensionContext, null, classTestDescriptor, configParams, null);
MethodExtensionContext methodExtensionContext = new MethodExtensionContext(classExtensionContext, null, methodTestDescriptor, configParams, testInstance, new ThrowableCollector());
// @formatter:off
assertAll("methodContext", () -> assertThat(methodExtensionContext.getElement()).contains(testMethod), () -> assertThat(methodExtensionContext.getTestClass()).contains(OuterClass.class), () -> assertThat(methodExtensionContext.getTestInstance()).contains(testInstance), () -> assertThat(methodExtensionContext.getTestMethod()).contains(testMethod), () -> assertThat(methodExtensionContext.getRequiredTestClass()).isEqualTo(OuterClass.class), () -> assertThat(methodExtensionContext.getRequiredTestInstance()).isEqualTo(testInstance), () -> assertThat(methodExtensionContext.getRequiredTestMethod()).isEqualTo(testMethod), () -> assertThat(methodExtensionContext.getDisplayName()).isEqualTo(methodTestDescriptor.getDisplayName()), () -> assertThat(methodExtensionContext.getParent()).contains(classExtensionContext), () -> assertThat(methodExtensionContext.getRoot()).isSameAs(engineExtensionContext));
// @formatter:on
}
use of org.junit.jupiter.engine.descriptor.ClassExtensionContext in project junit5 by junit-team.
the class ExtensionContextTests method usingStore.
@Test
@SuppressWarnings("resource")
void usingStore() {
TestMethodTestDescriptor methodTestDescriptor = methodDescriptor();
ClassTestDescriptor classTestDescriptor = outerClassDescriptor(methodTestDescriptor);
ExtensionContext parentContext = new ClassExtensionContext(null, null, classTestDescriptor, configParams, null);
MethodExtensionContext childContext = new MethodExtensionContext(parentContext, null, methodTestDescriptor, configParams, new OuterClass(), new ThrowableCollector());
ExtensionContext.Store childStore = childContext.getStore(Namespace.GLOBAL);
ExtensionContext.Store parentStore = parentContext.getStore(Namespace.GLOBAL);
final Object key1 = "key 1";
final String value1 = "a value";
childStore.put(key1, value1);
assertEquals(value1, childStore.get(key1));
assertEquals(value1, childStore.remove(key1));
assertNull(childStore.get(key1));
childStore.put(key1, value1);
assertEquals(value1, childStore.get(key1));
assertEquals(value1, childStore.remove(key1, String.class));
assertNull(childStore.get(key1));
final Object key2 = "key 2";
final String value2 = "other value";
assertEquals(value2, childStore.getOrComputeIfAbsent(key2, key -> value2));
assertEquals(value2, childStore.getOrComputeIfAbsent(key2, key -> value2, String.class));
assertEquals(value2, childStore.get(key2));
assertEquals(value2, childStore.get(key2, String.class));
final Object parentKey = "parent key";
final String parentValue = "parent value";
parentStore.put(parentKey, parentValue);
assertEquals(parentValue, childStore.getOrComputeIfAbsent(parentKey, k -> "a different value"));
assertEquals(parentValue, childStore.get(parentKey));
}
Aggregations