Search in sources :

Example 6 with ExtensionRegistry

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

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

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

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

the class ExecutableInvoker method resolveParameter.

private Object resolveParameter(ParameterContext parameterContext, Executable executable, ExtensionContext extensionContext, ExtensionRegistry extensionRegistry) {
    try {
        // @formatter:off
        List<ParameterResolver> matchingResolvers = extensionRegistry.stream(ParameterResolver.class).filter(resolver -> resolver.supportsParameter(parameterContext, extensionContext)).collect(toList());
        if (matchingResolvers.isEmpty()) {
            throw new ParameterResolutionException(String.format("No ParameterResolver registered for parameter [%s] in executable [%s].", parameterContext.getParameter(), executable.toGenericString()));
        }
        if (matchingResolvers.size() > 1) {
            // @formatter:off
            String resolverNames = matchingResolvers.stream().map(resolver -> resolver.getClass().getName()).collect(joining(", "));
            // @formatter:on
            throw new ParameterResolutionException(String.format("Discovered multiple competing ParameterResolvers for parameter [%s] in executable [%s]: %s", parameterContext.getParameter(), executable.toGenericString(), resolverNames));
        }
        ParameterResolver resolver = matchingResolvers.get(0);
        Object value = resolver.resolveParameter(parameterContext, extensionContext);
        validateResolvedType(parameterContext.getParameter(), value, executable, resolver);
        logger.trace(() -> String.format("ParameterResolver [%s] resolved a value of type [%s] for parameter [%s] in executable [%s].", resolver.getClass().getName(), (value != null ? value.getClass().getName() : null), parameterContext.getParameter(), executable.toGenericString()));
        return value;
    } catch (ParameterResolutionException ex) {
        throw ex;
    } catch (Throwable ex) {
        throw new ParameterResolutionException(String.format("Failed to resolve parameter [%s] in executable [%s]", parameterContext.getParameter(), executable.toGenericString()), ex);
    }
}
Also used : ParameterResolver(org.junit.jupiter.api.extension.ParameterResolver) Preconditions(org.junit.platform.commons.util.Preconditions) ReflectionUtils.isAssignableTo(org.junit.platform.commons.util.ReflectionUtils.isAssignableTo) Logger(org.junit.platform.commons.logging.Logger) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) API(org.apiguardian.api.API) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) INTERNAL(org.apiguardian.api.API.Status.INTERNAL) Constructor(java.lang.reflect.Constructor) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) Collectors.joining(java.util.stream.Collectors.joining) LoggerFactory(org.junit.platform.commons.logging.LoggerFactory) ParameterContext(org.junit.jupiter.api.extension.ParameterContext) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) Parameter(java.lang.reflect.Parameter) Executable(java.lang.reflect.Executable) Optional(java.util.Optional) Method(java.lang.reflect.Method) ParameterResolver(org.junit.jupiter.api.extension.ParameterResolver) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException)

Example 10 with ExtensionRegistry

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

the class UseDataProviderInvocationContextProvider 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