Search in sources :

Example 1 with ClassExtensionContext

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);
}
Also used : ClassExtensionContext(org.junit.jupiter.engine.descriptor.ClassExtensionContext) NestedClassTestDescriptor(org.junit.jupiter.engine.descriptor.NestedClassTestDescriptor) ClassTestDescriptor(org.junit.jupiter.engine.descriptor.ClassTestDescriptor) PreconditionViolationException(org.junit.platform.commons.util.PreconditionViolationException) DynamicTest.dynamicTest(org.junit.jupiter.api.DynamicTest.dynamicTest) Test(org.junit.jupiter.api.Test) DynamicTest(org.junit.jupiter.api.DynamicTest)

Example 2 with ClassExtensionContext

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))));
}
Also used : UniqueId(org.junit.platform.engine.UniqueId) Method(java.lang.reflect.Method) ConfigurationParameters(org.junit.platform.engine.ConfigurationParameters) NestedClassTestDescriptor(org.junit.jupiter.engine.descriptor.NestedClassTestDescriptor) ClassTestDescriptor(org.junit.jupiter.engine.descriptor.ClassTestDescriptor) JupiterEngineDescriptor(org.junit.jupiter.engine.descriptor.JupiterEngineDescriptor) MethodExtensionContext(org.junit.jupiter.engine.descriptor.MethodExtensionContext) ClassExtensionContext(org.junit.jupiter.engine.descriptor.ClassExtensionContext) MethodExtensionContext(org.junit.jupiter.engine.descriptor.MethodExtensionContext) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) JupiterEngineExtensionContext(org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext) ClassExtensionContext(org.junit.jupiter.engine.descriptor.ClassExtensionContext) JupiterEngineExtensionContext(org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext) TestMethodTestDescriptor(org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor) TestFactory(org.junit.jupiter.api.TestFactory)

Example 3 with ClassExtensionContext

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());
}
Also used : UniqueId(org.junit.platform.engine.UniqueId) ClassExtensionContext(org.junit.jupiter.engine.descriptor.ClassExtensionContext) ClassTestDescriptor(org.junit.jupiter.engine.descriptor.ClassTestDescriptor) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) Test(org.junit.jupiter.api.Test)

Example 4 with ClassExtensionContext

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
}
Also used : MethodExtensionContext(org.junit.jupiter.engine.descriptor.MethodExtensionContext) ClassExtensionContext(org.junit.jupiter.engine.descriptor.ClassExtensionContext) Method(java.lang.reflect.Method) NestedClassTestDescriptor(org.junit.jupiter.engine.descriptor.NestedClassTestDescriptor) ClassTestDescriptor(org.junit.jupiter.engine.descriptor.ClassTestDescriptor) JupiterEngineExtensionContext(org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext) TestMethodTestDescriptor(org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor) JupiterEngineDescriptor(org.junit.jupiter.engine.descriptor.JupiterEngineDescriptor) DynamicTest.dynamicTest(org.junit.jupiter.api.DynamicTest.dynamicTest) Test(org.junit.jupiter.api.Test) DynamicTest(org.junit.jupiter.api.DynamicTest)

Example 5 with ClassExtensionContext

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));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) TestFactory(org.junit.jupiter.api.TestFactory) NestedClassTestDescriptor(org.junit.jupiter.engine.descriptor.NestedClassTestDescriptor) ConfigurationParameters(org.junit.platform.engine.ConfigurationParameters) ReportEntry(org.junit.platform.engine.reporting.ReportEntry) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) MethodExtensionContext(org.junit.jupiter.engine.descriptor.MethodExtensionContext) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) EngineExecutionListener(org.junit.platform.engine.EngineExecutionListener) ArgumentCaptor(org.mockito.ArgumentCaptor) DynamicTest.dynamicTest(org.junit.jupiter.api.DynamicTest.dynamicTest) Map(java.util.Map) ClassTestDescriptor(org.junit.jupiter.engine.descriptor.ClassTestDescriptor) Assertions.assertAll(org.junit.jupiter.api.Assertions.assertAll) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Tag(org.junit.jupiter.api.Tag) Method(java.lang.reflect.Method) Namespace(org.junit.jupiter.api.extension.ExtensionContext.Namespace) JupiterEngineExtensionContext(org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext) TestMethodTestDescriptor(org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor) UniqueId(org.junit.platform.engine.UniqueId) ClassExtensionContext(org.junit.jupiter.engine.descriptor.ClassExtensionContext) Test(org.junit.jupiter.api.Test) PreconditionViolationException(org.junit.platform.commons.util.PreconditionViolationException) Mockito(org.mockito.Mockito) Stream(java.util.stream.Stream) JupiterEngineDescriptor(org.junit.jupiter.engine.descriptor.JupiterEngineDescriptor) DynamicTest(org.junit.jupiter.api.DynamicTest) Optional(java.util.Optional) TestDescriptor(org.junit.platform.engine.TestDescriptor) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) MethodExtensionContext(org.junit.jupiter.engine.descriptor.MethodExtensionContext) ClassExtensionContext(org.junit.jupiter.engine.descriptor.ClassExtensionContext) MethodExtensionContext(org.junit.jupiter.engine.descriptor.MethodExtensionContext) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) JupiterEngineExtensionContext(org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext) ClassExtensionContext(org.junit.jupiter.engine.descriptor.ClassExtensionContext) NestedClassTestDescriptor(org.junit.jupiter.engine.descriptor.NestedClassTestDescriptor) ClassTestDescriptor(org.junit.jupiter.engine.descriptor.ClassTestDescriptor) TestMethodTestDescriptor(org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor) DynamicTest.dynamicTest(org.junit.jupiter.api.DynamicTest.dynamicTest) Test(org.junit.jupiter.api.Test) DynamicTest(org.junit.jupiter.api.DynamicTest)

Aggregations

ClassExtensionContext (org.junit.jupiter.engine.descriptor.ClassExtensionContext)8 ClassTestDescriptor (org.junit.jupiter.engine.descriptor.ClassTestDescriptor)8 Test (org.junit.jupiter.api.Test)7 NestedClassTestDescriptor (org.junit.jupiter.engine.descriptor.NestedClassTestDescriptor)6 DynamicTest (org.junit.jupiter.api.DynamicTest)5 DynamicTest.dynamicTest (org.junit.jupiter.api.DynamicTest.dynamicTest)5 MethodExtensionContext (org.junit.jupiter.engine.descriptor.MethodExtensionContext)5 JupiterEngineExtensionContext (org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext)4 TestMethodTestDescriptor (org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor)4 UniqueId (org.junit.platform.engine.UniqueId)4 Method (java.lang.reflect.Method)3 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)3 JupiterEngineDescriptor (org.junit.jupiter.engine.descriptor.JupiterEngineDescriptor)3 TestFactory (org.junit.jupiter.api.TestFactory)2 ExtensionRegistry (org.junit.jupiter.engine.extension.ExtensionRegistry)2 PreconditionViolationException (org.junit.platform.commons.util.PreconditionViolationException)2 ConfigurationParameters (org.junit.platform.engine.ConfigurationParameters)2 EngineExecutionListener (org.junit.platform.engine.EngineExecutionListener)2 ReportEntry (org.junit.platform.engine.reporting.ReportEntry)2 Collections (java.util.Collections)1