Search in sources :

Example 1 with ParameterResolutionException

use of org.junit.jupiter.api.extension.ParameterResolutionException in project junit5 by junit-team.

the class ExecutableInvokerTests method reportIfThereAreMultipleParameterResolversThatSupportTheParameter.

@Test
void reportIfThereAreMultipleParameterResolversThatSupportTheParameter() {
    testMethodWithASingleStringParameter();
    thereIsAParameterResolverThatResolvesTheParameterTo("one");
    thereIsAParameterResolverThatResolvesTheParameterTo("two");
    ParameterResolutionException caught = assertThrows(ParameterResolutionException.class, this::invokeMethod);
    // @formatter:off
    assertThat(caught.getMessage()).contains("parameter [java.lang.String").contains(ConfigurableParameterResolver.class.getName() + ", " + ConfigurableParameterResolver.class.getName());
// @formatter:on
}
Also used : ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) Test(org.junit.jupiter.api.Test)

Example 2 with ParameterResolutionException

use of org.junit.jupiter.api.extension.ParameterResolutionException in project junit5 by junit-team.

the class ExecutableInvokerTests method doNotWrapThrownExceptionIfItIsAlreadyAParameterResolutionException.

@Test
void doNotWrapThrownExceptionIfItIsAlreadyAParameterResolutionException() {
    anyTestMethodWithAtLeastOneParameter();
    ParameterResolutionException cause = new ParameterResolutionException("custom message");
    throwDuringParameterResolution(cause);
    ParameterResolutionException caught = assertThrows(ParameterResolutionException.class, this::invokeMethod);
    assertSame(cause, caught);
}
Also used : ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) Test(org.junit.jupiter.api.Test)

Example 3 with ParameterResolutionException

use of org.junit.jupiter.api.extension.ParameterResolutionException in project junit5 by junit-team.

the class ExecutableInvokerTests method reportThatNullIsNotAViableArgumentIfAPrimitiveTypeIsExpected.

@Test
void reportThatNullIsNotAViableArgumentIfAPrimitiveTypeIsExpected() {
    testMethodWithASinglePrimitiveIntParameter();
    thereIsAParameterResolverThatResolvesTheParameterTo(null);
    ParameterResolutionException caught = assertThrows(ParameterResolutionException.class, this::invokeMethod);
    // @formatter:off
    assertThat(caught.getMessage()).contains("resolved a null value for parameter [int").contains("but a primitive of type [int] is required.");
// @formatter:on
}
Also used : ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) Test(org.junit.jupiter.api.Test)

Example 4 with ParameterResolutionException

use of org.junit.jupiter.api.extension.ParameterResolutionException in project junit5 by junit-team.

the class ParameterizedTestParameterResolver method resolveParameter.

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
    Object argument = arguments[parameterContext.getIndex()];
    Parameter parameter = parameterContext.getParameter();
    Optional<ConvertWith> annotation = AnnotationUtils.findAnnotation(parameter, ConvertWith.class);
    // @formatter:off
    ArgumentConverter argumentConverter = annotation.map(ConvertWith::value).map(clazz -> (ArgumentConverter) ReflectionUtils.newInstance(clazz)).map(converter -> AnnotationConsumerInitializer.initialize(parameter, converter)).orElse(DefaultArgumentConverter.INSTANCE);
    // @formatter:on
    try {
        return argumentConverter.convert(argument, parameterContext);
    } catch (Exception ex) {
        throw new ParameterResolutionException("Error resolving parameter at index " + parameterContext.getIndex(), ex);
    }
}
Also used : AnnotationConsumerInitializer(org.junit.jupiter.params.support.AnnotationConsumerInitializer) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) ParameterContext(org.junit.jupiter.api.extension.ParameterContext) ConvertWith(org.junit.jupiter.params.converter.ConvertWith) Parameter(java.lang.reflect.Parameter) Executable(java.lang.reflect.Executable) Optional(java.util.Optional) AnnotationUtils(org.junit.platform.commons.util.AnnotationUtils) Method(java.lang.reflect.Method) ParameterResolver(org.junit.jupiter.api.extension.ParameterResolver) ArgumentConverter(org.junit.jupiter.params.converter.ArgumentConverter) DefaultArgumentConverter(org.junit.jupiter.params.converter.DefaultArgumentConverter) ArgumentConverter(org.junit.jupiter.params.converter.ArgumentConverter) DefaultArgumentConverter(org.junit.jupiter.params.converter.DefaultArgumentConverter) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) ConvertWith(org.junit.jupiter.params.converter.ConvertWith) Parameter(java.lang.reflect.Parameter) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException)

Example 5 with ParameterResolutionException

use of org.junit.jupiter.api.extension.ParameterResolutionException 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)

Aggregations

ParameterResolutionException (org.junit.jupiter.api.extension.ParameterResolutionException)10 Test (org.junit.jupiter.api.Test)6 ExtensionRegistry (org.junit.jupiter.engine.extension.ExtensionRegistry)3 Executable (java.lang.reflect.Executable)2 Method (java.lang.reflect.Method)2 Parameter (java.lang.reflect.Parameter)2 Optional (java.util.Optional)2 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)2 Store (org.junit.jupiter.api.extension.ExtensionContext.Store)2 ParameterContext (org.junit.jupiter.api.extension.ParameterContext)2 ParameterResolver (org.junit.jupiter.api.extension.ParameterResolver)2 ReflectionUtils (org.junit.platform.commons.util.ReflectionUtils)2 Constructor (java.lang.reflect.Constructor)1 List (java.util.List)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 ArgumentConverter (org.junit.jupiter.params.converter.ArgumentConverter)1 ConvertWith (org.junit.jupiter.params.converter.ConvertWith)1