Search in sources :

Example 6 with ThrowableCollector

use of org.junit.jupiter.engine.execution.ThrowableCollector in project junit5 by junit-team.

the class ClassTestDescriptor method before.

@Override
public JupiterEngineExecutionContext before(JupiterEngineExecutionContext context) throws Exception {
    Lifecycle lifecycle = context.getExtensionContext().getTestInstanceLifecycle().orElse(Lifecycle.PER_METHOD);
    if (lifecycle == Lifecycle.PER_CLASS) {
        // Eagerly load test instance for BeforeAllCallbacks, if necessary,
        // and store the instance in the ExtensionContext.
        ClassExtensionContext extensionContext = (ClassExtensionContext) context.getExtensionContext();
        extensionContext.setTestInstance(context.getTestInstanceProvider().getTestInstance(Optional.empty()));
    }
    ThrowableCollector throwableCollector = context.getThrowableCollector();
    invokeBeforeAllCallbacks(context);
    if (throwableCollector.isEmpty()) {
        context.beforeAllMethodsExecuted(true);
        invokeBeforeAllMethods(context);
    }
    throwableCollector.assertEmpty();
    return context;
}
Also used : TestInstanceLifecycleUtils.getTestInstanceLifecycle(org.junit.jupiter.engine.descriptor.TestInstanceLifecycleUtils.getTestInstanceLifecycle) Lifecycle(org.junit.jupiter.api.TestInstance.Lifecycle) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector)

Example 7 with ThrowableCollector

use of org.junit.jupiter.engine.execution.ThrowableCollector 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 8 with ThrowableCollector

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

Example 9 with ThrowableCollector

use of org.junit.jupiter.engine.execution.ThrowableCollector in project junit5 by junit-team.

the class ClassTestDescriptor method invokeBeforeAllMethods.

private void invokeBeforeAllMethods(JupiterEngineExecutionContext context) {
    ExtensionRegistry registry = context.getExtensionRegistry();
    ExtensionContext extensionContext = context.getExtensionContext();
    ThrowableCollector throwableCollector = context.getThrowableCollector();
    Object testInstance = extensionContext.getTestInstance().orElse(null);
    for (Method method : this.beforeAllMethods) {
        throwableCollector.execute(() -> executableInvoker.invoke(method, testInstance, extensionContext, registry));
        if (throwableCollector.isNotEmpty()) {
            break;
        }
    }
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Method(java.lang.reflect.Method) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector)

Example 10 with ThrowableCollector

use of org.junit.jupiter.engine.execution.ThrowableCollector in project junit5 by junit-team.

the class TestMethodTestDescriptor method prepare.

// --- Node ----------------------------------------------------------------
@Override
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) throws Exception {
    ExtensionRegistry registry = populateNewExtensionRegistry(context);
    Object testInstance = context.getTestInstanceProvider().getTestInstance(Optional.of(registry));
    ThrowableCollector throwableCollector = new ThrowableCollector();
    ExtensionContext extensionContext = new MethodExtensionContext(context.getExtensionContext(), context.getExecutionListener(), this, context.getConfigurationParameters(), testInstance, throwableCollector);
    // @formatter:off
    return context.extend().withExtensionRegistry(registry).withExtensionContext(extensionContext).withThrowableCollector(throwableCollector).build();
// @formatter:on
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector)

Aggregations

ThrowableCollector (org.junit.jupiter.engine.execution.ThrowableCollector)12 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)9 ExtensionRegistry (org.junit.jupiter.engine.extension.ExtensionRegistry)8 Method (java.lang.reflect.Method)3 Lifecycle (org.junit.jupiter.api.TestInstance.Lifecycle)2 Executable (org.junit.jupiter.api.function.Executable)2 TestInstanceLifecycleUtils.getTestInstanceLifecycle (org.junit.jupiter.engine.descriptor.TestInstanceLifecycleUtils.getTestInstanceLifecycle)2 BeforeEach (org.junit.jupiter.api.BeforeEach)1 AfterAllCallback (org.junit.jupiter.api.extension.AfterAllCallback)1 BeforeAllCallback (org.junit.jupiter.api.extension.BeforeAllCallback)1 JupiterEngineExecutionContext (org.junit.jupiter.engine.execution.JupiterEngineExecutionContext)1