Search in sources :

Example 16 with ExtensionRegistry

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

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

the class JupiterEngineExecutionContextTests method extendWithAllAttributes.

@Test
void extendWithAllAttributes() {
    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);
    JupiterEngineExecutionContext newContext = // 
    originalContext.extend().withExtensionContext(// 
    extensionContext).withExtensionRegistry(// 
    extensionRegistry).withTestInstanceProvider(// 
    testInstanceProvider).build();
    assertSame(extensionContext, 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 18 with ExtensionRegistry

use of org.junit.jupiter.engine.extension.ExtensionRegistry in project junit-dataprovider by TNG.

the class AbstractUseDataProviderArgumentProvider method invokeDataProviderMethodToRetrieveData.

/**
 * Retrieves the test data from given dataprovider method.
 *
 * @param dataProviderMethod the dataprovider method that gives the parameters; never {@code null}
 * @param cacheDataProviderResult determines if the dataprovider result should be cached using
 *            {@code dataProviderMethod} as key
 * @param context the execution context to use to create a {@link TestInfo} if required; never {@code null}
 *
 * @return a list of methods, each method bound to a parameter combination returned by the dataprovider
 * @throws NullPointerException if and only if one of the given arguments is {@code null}
 */
protected Object invokeDataProviderMethodToRetrieveData(Method dataProviderMethod, boolean cacheDataProviderResult, ExtensionContext context) {
    checkNotNull(dataProviderMethod, "'dataProviderMethod' must not be null");
    checkNotNull(context, "'context' must not be null");
    Store store = context.getRoot().getStore(NAMESPACE_USE_DATAPROVIDER);
    Object cached = store.get(dataProviderMethod);
    if (cached != null) {
        return cached;
    }
    try {
        // TODO how to not require junit-jupiter-engine dependency and reuse already existing ExtensionRegistry?
        ExtensionRegistry extensionRegistry = createRegistryWithDefaultExtensions(emptyConfigurationParameters());
        Object data = executableInvoker.invoke(dataProviderMethod, context.getTestInstance().orElse(null), context, extensionRegistry);
        if (cacheDataProviderResult) {
            store.put(dataProviderMethod, data);
        }
        return data;
    } catch (Exception e) {
        throw new ParameterResolutionException(String.format("Exception while invoking dataprovider method '%s': %s", dataProviderMethod.getName(), e.getMessage()), e);
    }
}
Also used : ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) Store(org.junit.jupiter.api.extension.ExtensionContext.Store) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry)

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