use of org.junit.jupiter.engine.extension.ExtensionRegistry in project junit5 by junit-team.
the class ClassTestDescriptor method invokeAfterAllMethods.
private void invokeAfterAllMethods(JupiterEngineExecutionContext context) {
ExtensionRegistry registry = context.getExtensionRegistry();
ExtensionContext extensionContext = context.getExtensionContext();
ThrowableCollector throwableCollector = context.getThrowableCollector();
Object testInstance = extensionContext.getTestInstance().orElse(null);
this.afterAllMethods.forEach(method -> throwableCollector.execute(() -> executableInvoker.invoke(method, testInstance, extensionContext, registry)));
}
use of org.junit.jupiter.engine.extension.ExtensionRegistry in project junit5 by junit-team.
the class ClassTestDescriptor method invokeAfterAllCallbacks.
private void invokeAfterAllCallbacks(JupiterEngineExecutionContext context) {
ExtensionRegistry registry = context.getExtensionRegistry();
ExtensionContext extensionContext = context.getExtensionContext();
ThrowableCollector throwableCollector = context.getThrowableCollector();
//
registry.getReversedExtensions(AfterAllCallback.class).forEach(extension -> throwableCollector.execute(() -> extension.afterAll(extensionContext)));
}
use of org.junit.jupiter.engine.extension.ExtensionRegistry in project junit5 by junit-team.
the class ClassTestDescriptor method invokeBeforeAllMethods.
private void invokeBeforeAllMethods(JupiterEngineExecutionContext context) {
ExtensionRegistry registry = context.getExtensionRegistry();
ExtensionContext extensionContext = context.getExtensionContext();
ThrowableCollector throwableCollector = context.getThrowableCollector();
Object testInstance = extensionContext.getTestInstance().orElse(null);
for (Method method : this.beforeAllMethods) {
throwableCollector.execute(() -> executableInvoker.invoke(method, testInstance, extensionContext, registry));
if (throwableCollector.isNotEmpty()) {
break;
}
}
}
use of org.junit.jupiter.engine.extension.ExtensionRegistry 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);
}
}
use of org.junit.jupiter.engine.extension.ExtensionRegistry 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);
}
}
Aggregations