Search in sources :

Example 1 with MethodExtensionContext

use of org.junit.jupiter.engine.descriptor.MethodExtensionContext 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 2 with MethodExtensionContext

use of org.junit.jupiter.engine.descriptor.MethodExtensionContext 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 3 with MethodExtensionContext

use of org.junit.jupiter.engine.descriptor.MethodExtensionContext 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)

Example 4 with MethodExtensionContext

use of org.junit.jupiter.engine.descriptor.MethodExtensionContext in project junit5 by junit-team.

the class ExtensionContextTests method tagsCanBeRetrievedInExtensionContext.

@Test
@SuppressWarnings("resource")
void tagsCanBeRetrievedInExtensionContext() {
    ClassTestDescriptor nestedClassDescriptor = nestedClassDescriptor();
    ClassTestDescriptor outerClassDescriptor = outerClassDescriptor(nestedClassDescriptor);
    TestMethodTestDescriptor methodTestDescriptor = methodDescriptor();
    outerClassDescriptor.addChild(methodTestDescriptor);
    ClassExtensionContext outerExtensionContext = new ClassExtensionContext(null, null, outerClassDescriptor, configParams, null);
    assertThat(outerExtensionContext.getTags()).containsExactly("outer-tag");
    assertThat(outerExtensionContext.getRoot()).isSameAs(outerExtensionContext);
    ClassExtensionContext nestedExtensionContext = new ClassExtensionContext(outerExtensionContext, null, nestedClassDescriptor, configParams, null);
    assertThat(nestedExtensionContext.getTags()).containsExactlyInAnyOrder("outer-tag", "nested-tag");
    assertThat(nestedExtensionContext.getRoot()).isSameAs(outerExtensionContext);
    MethodExtensionContext methodExtensionContext = new MethodExtensionContext(outerExtensionContext, null, methodTestDescriptor, configParams, new OuterClass(), new ThrowableCollector());
    assertThat(methodExtensionContext.getTags()).containsExactlyInAnyOrder("outer-tag", "method-tag");
    assertThat(methodExtensionContext.getRoot()).isSameAs(outerExtensionContext);
}
Also used : MethodExtensionContext(org.junit.jupiter.engine.descriptor.MethodExtensionContext) 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)4 ClassTestDescriptor (org.junit.jupiter.engine.descriptor.ClassTestDescriptor)4 MethodExtensionContext (org.junit.jupiter.engine.descriptor.MethodExtensionContext)4 NestedClassTestDescriptor (org.junit.jupiter.engine.descriptor.NestedClassTestDescriptor)4 TestMethodTestDescriptor (org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor)4 Method (java.lang.reflect.Method)3 DynamicTest (org.junit.jupiter.api.DynamicTest)3 DynamicTest.dynamicTest (org.junit.jupiter.api.DynamicTest.dynamicTest)3 Test (org.junit.jupiter.api.Test)3 JupiterEngineDescriptor (org.junit.jupiter.engine.descriptor.JupiterEngineDescriptor)3 JupiterEngineExtensionContext (org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext)3 TestFactory (org.junit.jupiter.api.TestFactory)2 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)2 ConfigurationParameters (org.junit.platform.engine.ConfigurationParameters)2 UniqueId (org.junit.platform.engine.UniqueId)2 Collections (java.util.Collections)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Stream (java.util.stream.Stream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1