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
}
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);
}
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
}
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);
}
}
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);
}
}
Aggregations