Search in sources :

Example 1 with ParameterResolver

use of org.junit.jupiter.api.extension.ParameterResolver 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 2 with ParameterResolver

use of org.junit.jupiter.api.extension.ParameterResolver in project spring-framework by spring-projects.

the class SpringExtension method supportsParameter.

/**
 * Determine if the value for the {@link Parameter} in the supplied {@link ParameterContext}
 * should be autowired from the test's {@link ApplicationContext}.
 * <p>A parameter is considered to be autowirable if one of the following
 * conditions is {@code true}.
 * <ol>
 * <li>The {@linkplain ParameterContext#getDeclaringExecutable() declaring
 * executable} is a {@link Constructor} and
 * {@link TestConstructorUtils#isAutowirableConstructor(Constructor, Class, PropertyProvider)}
 * returns {@code true}. Note that {@code isAutowirableConstructor()} will be
 * invoked with a fallback {@link PropertyProvider} that delegates its lookup
 * to {@link ExtensionContext#getConfigurationParameter(String)}.</li>
 * <li>The parameter is of type {@link ApplicationContext} or a sub-type thereof.</li>
 * <li>The parameter is of type {@link ApplicationEvents} or a sub-type thereof.</li>
 * <li>{@link ParameterResolutionDelegate#isAutowirable} returns {@code true}.</li>
 * </ol>
 * <p><strong>WARNING</strong>: If a test class {@code Constructor} is annotated
 * with {@code @Autowired} or automatically autowirable (see {@link TestConstructor}),
 * Spring will assume the responsibility for resolving all parameters in the
 * constructor. Consequently, no other registered {@link ParameterResolver}
 * will be able to resolve parameters.
 * @see #resolveParameter
 * @see TestConstructorUtils#isAutowirableConstructor(Constructor, Class)
 * @see ParameterResolutionDelegate#isAutowirable
 */
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
    Parameter parameter = parameterContext.getParameter();
    Executable executable = parameter.getDeclaringExecutable();
    Class<?> testClass = extensionContext.getRequiredTestClass();
    PropertyProvider junitPropertyProvider = propertyName -> extensionContext.getConfigurationParameter(propertyName).orElse(null);
    return (TestConstructorUtils.isAutowirableConstructor(executable, testClass, junitPropertyProvider) || ApplicationContext.class.isAssignableFrom(parameter.getType()) || supportsApplicationEvents(parameterContext) || ParameterResolutionDelegate.isAutowirable(parameter, parameterContext.getIndex()));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) PropertyProvider(org.springframework.test.context.support.PropertyProvider) Autowired(org.springframework.beans.factory.annotation.Autowired) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ParameterResolutionDelegate(org.springframework.beans.factory.annotation.ParameterResolutionDelegate) Constructor(java.lang.reflect.Constructor) AfterAll(org.junit.jupiter.api.AfterAll) AfterEachCallback(org.junit.jupiter.api.extension.AfterEachCallback) MergedAnnotations(org.springframework.core.annotation.MergedAnnotations) BeforeAll(org.junit.jupiter.api.BeforeAll) Parameter(java.lang.reflect.Parameter) Executable(java.lang.reflect.Executable) Nullable(org.springframework.lang.Nullable) Method(java.lang.reflect.Method) Namespace(org.junit.jupiter.api.extension.ExtensionContext.Namespace) SearchStrategy(org.springframework.core.annotation.MergedAnnotations.SearchStrategy) TestContextManager(org.springframework.test.context.TestContextManager) TestConstructor(org.springframework.test.context.TestConstructor) MethodFilter(org.springframework.util.ReflectionUtils.MethodFilter) AfterTestExecutionCallback(org.junit.jupiter.api.extension.AfterTestExecutionCallback) Store(org.junit.jupiter.api.extension.ExtensionContext.Store) ApplicationContext(org.springframework.context.ApplicationContext) BeforeTestExecutionCallback(org.junit.jupiter.api.extension.BeforeTestExecutionCallback) ParameterContext(org.junit.jupiter.api.extension.ParameterContext) RepeatableContainers(org.springframework.core.annotation.RepeatableContainers) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) BeforeAllCallback(org.junit.jupiter.api.extension.BeforeAllCallback) AfterAllCallback(org.junit.jupiter.api.extension.AfterAllCallback) ReflectionUtils(org.springframework.util.ReflectionUtils) Modifier(java.lang.reflect.Modifier) Annotation(java.lang.annotation.Annotation) BeforeEachCallback(org.junit.jupiter.api.extension.BeforeEachCallback) Testable(org.junit.platform.commons.annotation.Testable) ApplicationEvents(org.springframework.test.context.event.ApplicationEvents) TestConstructorUtils(org.springframework.test.context.support.TestConstructorUtils) ParameterResolver(org.junit.jupiter.api.extension.ParameterResolver) TestInstancePostProcessor(org.junit.jupiter.api.extension.TestInstancePostProcessor) Assert(org.springframework.util.Assert) PropertyProvider(org.springframework.test.context.support.PropertyProvider) Parameter(java.lang.reflect.Parameter) Executable(java.lang.reflect.Executable)

Aggregations

Constructor (java.lang.reflect.Constructor)2 Executable (java.lang.reflect.Executable)2 Method (java.lang.reflect.Method)2 Parameter (java.lang.reflect.Parameter)2 List (java.util.List)2 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)2 ParameterContext (org.junit.jupiter.api.extension.ParameterContext)2 ParameterResolver (org.junit.jupiter.api.extension.ParameterResolver)2 Annotation (java.lang.annotation.Annotation)1 Modifier (java.lang.reflect.Modifier)1 Arrays (java.util.Arrays)1 Optional (java.util.Optional)1 Collectors.joining (java.util.stream.Collectors.joining)1 Collectors.toList (java.util.stream.Collectors.toList)1 API (org.apiguardian.api.API)1 INTERNAL (org.apiguardian.api.API.Status.INTERNAL)1 AfterAll (org.junit.jupiter.api.AfterAll)1 AfterEach (org.junit.jupiter.api.AfterEach)1 BeforeAll (org.junit.jupiter.api.BeforeAll)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1