Search in sources :

Example 11 with ExtensionContext

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

the class TestMethodTestDescriptor method invokeTestMethod.

protected void invokeTestMethod(JupiterEngineExecutionContext context, DynamicTestExecutor dynamicTestExecutor) {
    ExtensionContext extensionContext = context.getExtensionContext();
    ThrowableCollector throwableCollector = context.getThrowableCollector();
    throwableCollector.execute(() -> {
        try {
            Method testMethod = getTestMethod();
            Object instance = extensionContext.getRequiredTestInstance();
            executableInvoker.invoke(testMethod, instance, extensionContext, context.getExtensionRegistry());
        } catch (Throwable throwable) {
            invokeTestExecutionExceptionHandlers(context.getExtensionRegistry(), extensionContext, throwable);
        }
    });
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Method(java.lang.reflect.Method) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector)

Example 12 with ExtensionContext

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

the class TestTemplateTestDescriptor method execute.

@Override
public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext context, DynamicTestExecutor dynamicTestExecutor) throws Exception {
    ExtensionContext extensionContext = context.getExtensionContext();
    List<TestTemplateInvocationContextProvider> providers = validateProviders(extensionContext, context.getExtensionRegistry());
    AtomicInteger invocationIndex = new AtomicInteger();
    // @formatter:off
    providers.stream().flatMap(provider -> provider.provideTestTemplateInvocationContexts(extensionContext)).map(invocationContext -> createInvocationTestDescriptor(invocationContext, invocationIndex.incrementAndGet())).filter(Optional::isPresent).map(Optional::get).forEach(invocationTestDescriptor -> execute(dynamicTestExecutor, invocationTestDescriptor));
    // @formatter:on
    validateWasAtLeastInvokedOnce(invocationIndex.get());
    return context;
}
Also used : Preconditions(org.junit.platform.commons.util.Preconditions) ExtensionUtils.populateNewExtensionRegistryFromExtendWithAnnotation(org.junit.jupiter.engine.descriptor.ExtensionUtils.populateNewExtensionRegistryFromExtendWithAnnotation) API(org.apiguardian.api.API) UniqueId(org.junit.platform.engine.UniqueId) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) INTERNAL(org.apiguardian.api.API.Status.INTERNAL) JupiterEngineExecutionContext(org.junit.jupiter.engine.execution.JupiterEngineExecutionContext) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) TestTemplateInvocationContext(org.junit.jupiter.api.extension.TestTemplateInvocationContext) Optional(java.util.Optional) TestTemplateInvocationContextProvider(org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider) TestDescriptor(org.junit.platform.engine.TestDescriptor) Method(java.lang.reflect.Method) Optional(java.util.Optional) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) TestTemplateInvocationContextProvider(org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider)

Example 13 with ExtensionContext

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

the class ClassTestDescriptor method invokeBeforeAllCallbacks.

private void invokeBeforeAllCallbacks(JupiterEngineExecutionContext context) {
    ExtensionRegistry registry = context.getExtensionRegistry();
    ExtensionContext extensionContext = context.getExtensionContext();
    ThrowableCollector throwableCollector = context.getThrowableCollector();
    for (BeforeAllCallback callback : registry.getExtensions(BeforeAllCallback.class)) {
        throwableCollector.execute(() -> callback.beforeAll(extensionContext));
        if (throwableCollector.isNotEmpty()) {
            break;
        }
    }
}
Also used : BeforeAllCallback(org.junit.jupiter.api.extension.BeforeAllCallback) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector)

Example 14 with ExtensionContext

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

the class ClassTestDescriptor method invokeAfterAllMethods.

private void invokeAfterAllMethods(JupiterEngineExecutionContext context) {
    ExtensionRegistry registry = context.getExtensionRegistry();
    ExtensionContext extensionContext = context.getExtensionContext();
    ThrowableCollector throwableCollector = context.getThrowableCollector();
    Object testInstance = extensionContext.getTestInstance().orElse(null);
    this.afterAllMethods.forEach(method -> throwableCollector.execute(() -> executableInvoker.invoke(method, testInstance, extensionContext, registry)));
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector)

Example 15 with ExtensionContext

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

the class ClassTestDescriptor method invokeAfterAllCallbacks.

private void invokeAfterAllCallbacks(JupiterEngineExecutionContext context) {
    ExtensionRegistry registry = context.getExtensionRegistry();
    ExtensionContext extensionContext = context.getExtensionContext();
    ThrowableCollector throwableCollector = context.getThrowableCollector();
    // 
    registry.getReversedExtensions(AfterAllCallback.class).forEach(extension -> throwableCollector.execute(() -> extension.afterAll(extensionContext)));
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) AfterAllCallback(org.junit.jupiter.api.extension.AfterAllCallback) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector)

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