Search in sources :

Example 6 with ParameterResolutionException

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);
    }
}
Also used : ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) Store(org.junit.jupiter.api.extension.ExtensionContext.Store) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry)

Example 7 with ParameterResolutionException

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
}
Also used : ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) Test(org.junit.jupiter.api.Test)

Example 8 with ParameterResolutionException

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");
}
Also used : ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) Test(org.junit.jupiter.api.Test)

Example 9 with ParameterResolutionException

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");
}
Also used : ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) Test(org.junit.jupiter.api.Test)

Example 10 with ParameterResolutionException

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);
    }
}
Also used : ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) Store(org.junit.jupiter.api.extension.ExtensionContext.Store) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry)

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