Search in sources :

Example 1 with ThrowableCollector

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

the class TestFactoryTestDescriptorTests method before.

@BeforeEach
void before() throws Exception {
    extensionContext = mock(ExtensionContext.class);
    isClosed = false;
    context = new JupiterEngineExecutionContext(null, null).extend().withThrowableCollector(new ThrowableCollector()).withExtensionContext(extensionContext).build();
    Method testMethod = CustomStreamTestCase.class.getDeclaredMethod("customStream");
    descriptor = new TestFactoryTestDescriptor(UniqueId.forEngine("engine"), CustomStreamTestCase.class, testMethod);
    when(extensionContext.getTestMethod()).thenReturn(Optional.of(testMethod));
}
Also used : JupiterEngineExecutionContext(org.junit.jupiter.engine.execution.JupiterEngineExecutionContext) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Method(java.lang.reflect.Method) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with ThrowableCollector

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

the class TestMethodTestDescriptor method invokeAllAfterMethodsOrCallbacks.

private <T extends Extension> void invokeAllAfterMethodsOrCallbacks(JupiterEngineExecutionContext context, BiFunction<ExtensionContext, T, Executable> generator, Class<T> type) {
    ExtensionRegistry registry = context.getExtensionRegistry();
    ExtensionContext extensionContext = context.getExtensionContext();
    ThrowableCollector throwableCollector = context.getThrowableCollector();
    registry.getReversedExtensions(type).forEach(callback -> {
        Executable executable = generator.apply(extensionContext, callback);
        throwableCollector.execute(executable);
    });
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Executable(org.junit.jupiter.api.function.Executable) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector)

Example 3 with ThrowableCollector

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

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

the class ClassTestDescriptor method prepare.

// --- Node ----------------------------------------------------------------
@Override
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) {
    ExtensionRegistry registry = populateNewExtensionRegistryFromExtendWithAnnotation(context.getExtensionRegistry(), this.testClass);
    // Register extensions from static fields here, at the class level but
    // after extensions registered via @ExtendWith.
    registerExtensionsFromFields(registry, this.testClass, null);
    registerBeforeEachMethodAdapters(registry);
    registerAfterEachMethodAdapters(registry);
    Lifecycle lifecycle = getTestInstanceLifecycle(this.testClass, context.getConfigurationParameters());
    ThrowableCollector throwableCollector = new ThrowableCollector();
    ClassExtensionContext extensionContext = new ClassExtensionContext(context.getExtensionContext(), context.getExecutionListener(), this, lifecycle, context.getConfigurationParameters(), throwableCollector);
    this.beforeAllMethods = findBeforeAllMethods(this.testClass, lifecycle == Lifecycle.PER_METHOD);
    this.afterAllMethods = findAfterAllMethods(this.testClass, lifecycle == Lifecycle.PER_METHOD);
    // @formatter:off
    return context.extend().withTestInstanceProvider(testInstanceProvider(context, registry, extensionContext)).withExtensionRegistry(registry).withExtensionContext(extensionContext).withThrowableCollector(throwableCollector).build();
// @formatter:on
}
Also used : TestInstanceLifecycleUtils.getTestInstanceLifecycle(org.junit.jupiter.engine.descriptor.TestInstanceLifecycleUtils.getTestInstanceLifecycle) Lifecycle(org.junit.jupiter.api.TestInstance.Lifecycle) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector)

Example 5 with ThrowableCollector

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

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