Search in sources :

Example 26 with ExtensionContext

use of org.junit.jupiter.api.extension.ExtensionContext in project junit5 by junit-team.

the class ParameterizedTestExtensionTests method supportsReturnsFalseForTestMethodWithoutParameterizedTestAnnotation.

@Test
void supportsReturnsFalseForTestMethodWithoutParameterizedTestAnnotation() {
    ExtensionContext extensionContextWithUnAnnotatedTestMethod = getExtensionContextReturningSingleMethod(new TestCaseWithMethod());
    assertFalse(this.parameterizedTestExtension.supportsTestTemplate(extensionContextWithUnAnnotatedTestMethod));
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Test(org.junit.jupiter.api.Test)

Example 27 with ExtensionContext

use of org.junit.jupiter.api.extension.ExtensionContext in project junit5 by junit-team.

the class ParameterizedTestExtensionTests method supportsReturnsTrueForTestMethodWithParameterizedTestAnnotation.

@Test
void supportsReturnsTrueForTestMethodWithParameterizedTestAnnotation() {
    ExtensionContext extensionContextWithAnnotatedTestMethod = getExtensionContextReturningSingleMethod(new TestCaseWithAnnotatedMethod());
    assertTrue(this.parameterizedTestExtension.supportsTestTemplate(extensionContextWithAnnotatedTestMethod));
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Test(org.junit.jupiter.api.Test)

Example 28 with ExtensionContext

use of org.junit.jupiter.api.extension.ExtensionContext in project junit5 by junit-team.

the class Heavyweight method resolveParameter.

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext context) {
    ExtensionContext engineContext = context.getRoot();
    Store store = engineContext.getStore(ExtensionContext.Namespace.GLOBAL);
    ResourceValue resource = store.getOrComputeIfAbsent(ResourceValue.class);
    resource.usages.incrementAndGet();
    return resource;
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Store(org.junit.jupiter.api.extension.ExtensionContext.Store)

Example 29 with ExtensionContext

use of org.junit.jupiter.api.extension.ExtensionContext 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 30 with ExtensionContext

use of org.junit.jupiter.api.extension.ExtensionContext in project junit5 by junit-team.

the class ExtensionContextTests method reportEntriesArePublishedToExecutionContext.

@Test
@SuppressWarnings("resource")
void reportEntriesArePublishedToExecutionContext() {
    ClassTestDescriptor classTestDescriptor = outerClassDescriptor(null);
    EngineExecutionListener engineExecutionListener = Mockito.spy(EngineExecutionListener.class);
    ExtensionContext extensionContext = new ClassExtensionContext(null, engineExecutionListener, classTestDescriptor, configParams, null);
    Map<String, String> map1 = Collections.singletonMap("key", "value");
    Map<String, String> map2 = Collections.singletonMap("other key", "other value");
    extensionContext.publishReportEntry(map1);
    extensionContext.publishReportEntry(map2);
    extensionContext.publishReportEntry("3rd key", "third value");
    ArgumentCaptor<ReportEntry> entryCaptor = ArgumentCaptor.forClass(ReportEntry.class);
    Mockito.verify(engineExecutionListener, Mockito.times(3)).reportingEntryPublished(Mockito.eq(classTestDescriptor), entryCaptor.capture());
    ReportEntry reportEntry1 = entryCaptor.getAllValues().get(0);
    ReportEntry reportEntry2 = entryCaptor.getAllValues().get(1);
    ReportEntry reportEntry3 = entryCaptor.getAllValues().get(2);
    assertEquals(map1, reportEntry1.getKeyValuePairs());
    assertEquals(map2, reportEntry2.getKeyValuePairs());
    assertEquals("third value", reportEntry3.getKeyValuePairs().get("3rd key"));
}
Also used : EngineExecutionListener(org.junit.platform.engine.EngineExecutionListener) ClassExtensionContext(org.junit.jupiter.engine.descriptor.ClassExtensionContext) ReportEntry(org.junit.platform.engine.reporting.ReportEntry) 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) DynamicTest.dynamicTest(org.junit.jupiter.api.DynamicTest.dynamicTest) Test(org.junit.jupiter.api.Test) DynamicTest(org.junit.jupiter.api.DynamicTest)

Aggregations

ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)37 ExtensionRegistry (org.junit.jupiter.engine.extension.ExtensionRegistry)13 Method (java.lang.reflect.Method)11 ThrowableCollector (org.junit.jupiter.engine.execution.ThrowableCollector)11 Test (org.junit.jupiter.api.Test)10 Optional (java.util.Optional)7 UniqueId (org.junit.platform.engine.UniqueId)5 List (java.util.List)4 Stream (java.util.stream.Stream)4 API (org.apiguardian.api.API)4 INTERNAL (org.apiguardian.api.API.Status.INTERNAL)4 Executable (org.junit.jupiter.api.function.Executable)4 JupiterEngineExecutionContext (org.junit.jupiter.engine.execution.JupiterEngineExecutionContext)4 Preconditions (org.junit.platform.commons.util.Preconditions)4 Arrays (java.util.Arrays)3 AfterEachCallback (org.junit.jupiter.api.extension.AfterEachCallback)3 BeforeEachCallback (org.junit.jupiter.api.extension.BeforeEachCallback)3 TestTemplateInvocationContext (org.junit.jupiter.api.extension.TestTemplateInvocationContext)3 ClassExtensionContext (org.junit.jupiter.engine.descriptor.ClassExtensionContext)3 JUnitException (org.junit.platform.commons.JUnitException)3