Search in sources :

Example 1 with ConvertWith

use of org.junit.jupiter.params.converter.ConvertWith 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)

Aggregations

Executable (java.lang.reflect.Executable)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 Optional (java.util.Optional)1 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)1 ParameterContext (org.junit.jupiter.api.extension.ParameterContext)1 ParameterResolutionException (org.junit.jupiter.api.extension.ParameterResolutionException)1 ParameterResolver (org.junit.jupiter.api.extension.ParameterResolver)1 ArgumentConverter (org.junit.jupiter.params.converter.ArgumentConverter)1 ConvertWith (org.junit.jupiter.params.converter.ConvertWith)1 DefaultArgumentConverter (org.junit.jupiter.params.converter.DefaultArgumentConverter)1 AnnotationConsumerInitializer (org.junit.jupiter.params.support.AnnotationConsumerInitializer)1 AnnotationUtils (org.junit.platform.commons.util.AnnotationUtils)1 ReflectionUtils (org.junit.platform.commons.util.ReflectionUtils)1