use of org.junit.jupiter.api.extension.ParameterResolutionException 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);
}
}
use of org.junit.jupiter.api.extension.ParameterResolutionException in project junit5 by junit-team.
the class ExecutableInvokerTests method reportTypeMismatchBetweenParameterAndResolvedParameter.
@Test
void reportTypeMismatchBetweenParameterAndResolvedParameter() {
testMethodWithASingleStringParameter();
thereIsAParameterResolverThatResolvesTheParameterTo(BigDecimal.ONE);
ParameterResolutionException caught = assertThrows(ParameterResolutionException.class, this::invokeMethod);
// @formatter:off
assertThat(caught.getMessage()).contains("resolved a value of type [java.math.BigDecimal] for parameter [java.lang.String").contains("but a value assignment compatible with [java.lang.String] is required.");
// @formatter:on
}
use of org.junit.jupiter.api.extension.ParameterResolutionException in project junit5 by junit-team.
the class ExecutableInvokerTests method wrapAllExceptionsThrownDuringParameterResolutionIntoAParameterResolutionException.
@Test
void wrapAllExceptionsThrownDuringParameterResolutionIntoAParameterResolutionException() {
anyTestMethodWithAtLeastOneParameter();
IllegalArgumentException cause = anyExceptionButParameterResolutionException();
throwDuringParameterResolution(cause);
ParameterResolutionException caught = assertThrows(ParameterResolutionException.class, this::invokeMethod);
assertSame(cause, caught.getCause(), () -> "cause should be present");
assertThat(caught.getMessage()).startsWith("Failed to resolve parameter [java.lang.String");
}
use of org.junit.jupiter.api.extension.ParameterResolutionException in project junit5 by junit-team.
the class ExecutableInvokerTests method reportIfThereIsNoParameterResolverThatSupportsTheParameter.
@Test
void reportIfThereIsNoParameterResolverThatSupportsTheParameter() {
testMethodWithASingleStringParameter();
ParameterResolutionException caught = assertThrows(ParameterResolutionException.class, this::invokeMethod);
assertThat(caught.getMessage()).contains("parameter [java.lang.String");
}
use of org.junit.jupiter.api.extension.ParameterResolutionException in project junit-dataprovider by TNG.
the class AbstractUseDataProviderArgumentProvider 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);
}
}
Aggregations