Search in sources :

Example 1 with ExtensionRegistry

use of org.junit.jupiter.engine.extension.ExtensionRegistry in project junit5 by junit-team.

the class JupiterEngineExecutionContextTests method canOverrideAttributeWhenContextIsExtended.

@Test
void canOverrideAttributeWhenContextIsExtended() {
    UniqueId uniqueId = UniqueId.parse("[engine:junit-jupiter]/[class:MyClass]");
    ClassTestDescriptor classTestDescriptor = new ClassTestDescriptor(uniqueId, getClass());
    ClassExtensionContext extensionContext = new ClassExtensionContext(null, null, classTestDescriptor, configParams, null);
    ExtensionRegistry extensionRegistry = ExtensionRegistry.createRegistryWithDefaultExtensions(configParams);
    TestInstanceProvider testInstanceProvider = mock(TestInstanceProvider.class);
    ClassExtensionContext newExtensionContext = new ClassExtensionContext(extensionContext, null, classTestDescriptor, configParams, null);
    JupiterEngineExecutionContext newContext = // 
    originalContext.extend().withExtensionContext(// 
    extensionContext).withExtensionRegistry(// 
    extensionRegistry).withTestInstanceProvider(// 
    testInstanceProvider).build().extend().withExtensionContext(// 
    newExtensionContext).build();
    assertSame(newExtensionContext, newContext.getExtensionContext());
    assertSame(extensionRegistry, newContext.getExtensionRegistry());
    assertSame(testInstanceProvider, newContext.getTestInstanceProvider());
}
Also used : UniqueId(org.junit.platform.engine.UniqueId) ClassExtensionContext(org.junit.jupiter.engine.descriptor.ClassExtensionContext) ClassTestDescriptor(org.junit.jupiter.engine.descriptor.ClassTestDescriptor) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) Test(org.junit.jupiter.api.Test)

Example 2 with ExtensionRegistry

use of org.junit.jupiter.engine.extension.ExtensionRegistry 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 ExtensionRegistry

use of org.junit.jupiter.engine.extension.ExtensionRegistry in project junit5 by junit-team.

the class TestTemplateInvocationTestDescriptor method populateNewExtensionRegistry.

@Override
protected ExtensionRegistry populateNewExtensionRegistry(JupiterEngineExecutionContext context) {
    ExtensionRegistry registry = super.populateNewExtensionRegistry(context);
    invocationContext.getAdditionalExtensions().forEach(extension -> registry.registerExtension(extension, invocationContext));
    return registry;
}
Also used : ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry)

Example 4 with ExtensionRegistry

use of org.junit.jupiter.engine.extension.ExtensionRegistry 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 ExtensionRegistry

use of org.junit.jupiter.engine.extension.ExtensionRegistry 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

ExtensionRegistry (org.junit.jupiter.engine.extension.ExtensionRegistry)18 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)12 ThrowableCollector (org.junit.jupiter.engine.execution.ThrowableCollector)10 Method (java.lang.reflect.Method)4 Executable (org.junit.jupiter.api.function.Executable)4 UniqueId (org.junit.platform.engine.UniqueId)4 List (java.util.List)3 Optional (java.util.Optional)3 API (org.apiguardian.api.API)3 INTERNAL (org.apiguardian.api.API.Status.INTERNAL)3 ParameterResolutionException (org.junit.jupiter.api.extension.ParameterResolutionException)3 BiFunction (java.util.function.BiFunction)2 Test (org.junit.jupiter.api.Test)2 AfterEachCallback (org.junit.jupiter.api.extension.AfterEachCallback)2 AfterTestExecutionCallback (org.junit.jupiter.api.extension.AfterTestExecutionCallback)2 BeforeEachCallback (org.junit.jupiter.api.extension.BeforeEachCallback)2 BeforeTestExecutionCallback (org.junit.jupiter.api.extension.BeforeTestExecutionCallback)2 Extension (org.junit.jupiter.api.extension.Extension)2 Store (org.junit.jupiter.api.extension.ExtensionContext.Store)2 TestExecutionExceptionHandler (org.junit.jupiter.api.extension.TestExecutionExceptionHandler)2