Search in sources :

Example 21 with ExtensionContext

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

the class TestMethodTestDescriptor method invokeAfterEachMethods.

private void invokeAfterEachMethods(JupiterEngineExecutionContext context) {
    ExtensionRegistry registry = context.getExtensionRegistry();
    invokeAllAfterMethodsOrCallbacks(context, ((extensionContext, adapter) -> () -> adapter.invokeAfterEachMethod(extensionContext, registry)), AfterEachMethodAdapter.class);
}
Also used : ExecutableInvoker(org.junit.jupiter.engine.execution.ExecutableInvoker) AfterEachMethodAdapter(org.junit.jupiter.engine.execution.AfterEachMethodAdapter) ExtensionUtils.populateNewExtensionRegistryFromExtendWithAnnotation(org.junit.jupiter.engine.descriptor.ExtensionUtils.populateNewExtensionRegistryFromExtendWithAnnotation) BiFunction(java.util.function.BiFunction) API(org.apiguardian.api.API) UniqueId(org.junit.platform.engine.UniqueId) AfterTestExecutionCallback(org.junit.jupiter.api.extension.AfterTestExecutionCallback) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) BeforeEachMethodAdapter(org.junit.jupiter.engine.execution.BeforeEachMethodAdapter) INTERNAL(org.apiguardian.api.API.Status.INTERNAL) BeforeTestExecutionCallback(org.junit.jupiter.api.extension.BeforeTestExecutionCallback) Extension(org.junit.jupiter.api.extension.Extension) JupiterEngineExecutionContext(org.junit.jupiter.engine.execution.JupiterEngineExecutionContext) List(java.util.List) AfterEachCallback(org.junit.jupiter.api.extension.AfterEachCallback) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) Executable(org.junit.jupiter.api.function.Executable) ExceptionUtils(org.junit.platform.commons.util.ExceptionUtils) BeforeEachCallback(org.junit.jupiter.api.extension.BeforeEachCallback) Optional(java.util.Optional) TestDescriptor(org.junit.platform.engine.TestDescriptor) Method(java.lang.reflect.Method) TestExecutionExceptionHandler(org.junit.jupiter.api.extension.TestExecutionExceptionHandler) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry)

Example 22 with ExtensionContext

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

the class TestMethodTestDescriptor method invokeBeforeEachMethods.

private void invokeBeforeEachMethods(JupiterEngineExecutionContext context) {
    ExtensionRegistry registry = context.getExtensionRegistry();
    invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(context, ((extensionContext, adapter) -> () -> adapter.invokeBeforeEachMethod(extensionContext, registry)), BeforeEachMethodAdapter.class);
}
Also used : ExecutableInvoker(org.junit.jupiter.engine.execution.ExecutableInvoker) AfterEachMethodAdapter(org.junit.jupiter.engine.execution.AfterEachMethodAdapter) ExtensionUtils.populateNewExtensionRegistryFromExtendWithAnnotation(org.junit.jupiter.engine.descriptor.ExtensionUtils.populateNewExtensionRegistryFromExtendWithAnnotation) BiFunction(java.util.function.BiFunction) API(org.apiguardian.api.API) UniqueId(org.junit.platform.engine.UniqueId) AfterTestExecutionCallback(org.junit.jupiter.api.extension.AfterTestExecutionCallback) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) BeforeEachMethodAdapter(org.junit.jupiter.engine.execution.BeforeEachMethodAdapter) INTERNAL(org.apiguardian.api.API.Status.INTERNAL) BeforeTestExecutionCallback(org.junit.jupiter.api.extension.BeforeTestExecutionCallback) Extension(org.junit.jupiter.api.extension.Extension) JupiterEngineExecutionContext(org.junit.jupiter.engine.execution.JupiterEngineExecutionContext) List(java.util.List) AfterEachCallback(org.junit.jupiter.api.extension.AfterEachCallback) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) Executable(org.junit.jupiter.api.function.Executable) ExceptionUtils(org.junit.platform.commons.util.ExceptionUtils) BeforeEachCallback(org.junit.jupiter.api.extension.BeforeEachCallback) Optional(java.util.Optional) TestDescriptor(org.junit.platform.engine.TestDescriptor) Method(java.lang.reflect.Method) TestExecutionExceptionHandler(org.junit.jupiter.api.extension.TestExecutionExceptionHandler) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry)

Example 23 with ExtensionContext

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

the class TestTemplateTestDescriptor method prepare.

// --- Node ----------------------------------------------------------------
@Override
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) throws Exception {
    ExtensionRegistry registry = populateNewExtensionRegistryFromExtendWithAnnotation(context.getExtensionRegistry(), getTestMethod());
    // The test instance should be properly maintained by the enclosing class's ExtensionContext.
    Object testInstance = context.getExtensionContext().getTestInstance().orElse(null);
    ExtensionContext extensionContext = new TestTemplateExtensionContext(context.getExtensionContext(), context.getExecutionListener(), this, context.getConfigurationParameters(), testInstance);
    // @formatter:off
    return context.extend().withExtensionRegistry(registry).withExtensionContext(extensionContext).build();
// @formatter:on
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry)

Example 24 with ExtensionContext

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

the class JupiterEngineDescriptor method prepare.

@Override
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) {
    ExtensionRegistry extensionRegistry = createRegistryWithDefaultExtensions(context.getConfigurationParameters());
    EngineExecutionListener executionListener = context.getExecutionListener();
    ExtensionContext extensionContext = new JupiterEngineExtensionContext(executionListener, this, context.getConfigurationParameters());
    // @formatter:off
    return context.extend().withExtensionRegistry(extensionRegistry).withExtensionContext(extensionContext).build();
// @formatter:on
}
Also used : EngineExecutionListener(org.junit.platform.engine.EngineExecutionListener) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry)

Example 25 with ExtensionContext

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

the class TestFactoryTestDescriptor method invokeTestMethod.

// --- Node ----------------------------------------------------------------
@Override
protected void invokeTestMethod(JupiterEngineExecutionContext context, DynamicTestExecutor dynamicTestExecutor) {
    ExtensionContext extensionContext = context.getExtensionContext();
    context.getThrowableCollector().execute(() -> {
        Object instance = extensionContext.getRequiredTestInstance();
        Object testFactoryMethodResult = executableInvoker.invoke(getTestMethod(), instance, extensionContext, context.getExtensionRegistry());
        TestSource source = getSource().orElseThrow(() -> new JUnitException("Illegal state: TestSource must be present"));
        try (Stream<DynamicNode> dynamicNodeStream = toDynamicNodeStream(testFactoryMethodResult)) {
            int index = 1;
            Iterator<DynamicNode> iterator = dynamicNodeStream.iterator();
            while (iterator.hasNext()) {
                DynamicNode dynamicNode = iterator.next();
                Optional<JupiterTestDescriptor> descriptor = createDynamicDescriptor(this, dynamicNode, index++, source, getDynamicDescendantFilter());
                descriptor.ifPresent(dynamicTestExecutor::execute);
            }
        } catch (ClassCastException ex) {
            throw invalidReturnTypeException(ex);
        }
    });
}
Also used : TestSource(org.junit.platform.engine.TestSource) JUnitException(org.junit.platform.commons.JUnitException) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) DynamicNode(org.junit.jupiter.api.DynamicNode)

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