use of org.junit.jupiter.api.extension.ExtensionContext 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.api.extension.ExtensionContext 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.api.extension.ExtensionContext in project component-runtime by Talend.
the class JUnit5HttpApi method beforeAll.
@Override
public void beforeAll(final ExtensionContext extensionContext) {
final HttpApi config = extensionContext.getElement().map(c -> c.getAnnotation(HttpApi.class)).orElse(null);
if (config != null) {
setGlobalProxyConfiguration(config.globalProxyConfiguration());
setLogLevel(config.logLevel());
setPort(config.port());
newInstance(config.responseLocator(), ResponseLocator.class).ifPresent(this::setResponseLocator);
newInstance(config.headerFilter(), Predicate.class).ifPresent(this::setHeaderFilter);
newInstance(config.executor(), Executor.class).ifPresent(this::setExecutor);
newInstance(config.sslContext(), Supplier.class).map(s -> SSLContext.class.cast(s.get())).ifPresent(this::setSslContext);
setSkipProxyHeaders(config.skipProxyHeaders());
if (config.useSsl()) {
activeSsl();
}
}
extensionContext.getStore(NAMESPACE).put(HttpApiHandler.class.getName(), this);
final HandlerImpl<JUnit5HttpApi> handler = new HandlerImpl<>(this, null, null);
extensionContext.getStore(NAMESPACE).put(HandlerImpl.class.getName(), handler);
handler.start();
}
use of org.junit.jupiter.api.extension.ExtensionContext in project junit5 by junit-team.
the class TestMethodTestDescriptor method prepare.
// --- Node ----------------------------------------------------------------
@Override
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) throws Exception {
ExtensionRegistry registry = populateNewExtensionRegistry(context);
Object testInstance = context.getTestInstanceProvider().getTestInstance(Optional.of(registry));
ThrowableCollector throwableCollector = new ThrowableCollector();
ExtensionContext extensionContext = new MethodExtensionContext(context.getExtensionContext(), context.getExecutionListener(), this, context.getConfigurationParameters(), testInstance, throwableCollector);
// @formatter:off
return context.extend().withExtensionRegistry(registry).withExtensionContext(extensionContext).withThrowableCollector(throwableCollector).build();
// @formatter:on
}
use of org.junit.jupiter.api.extension.ExtensionContext in project junit5 by junit-team.
the class TestMethodTestDescriptor method invokeBeforeMethodsOrCallbacksUntilExceptionOccurs.
private <T extends Extension> void invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(JupiterEngineExecutionContext context, BiFunction<ExtensionContext, T, Executable> generator, Class<T> type) {
ExtensionRegistry registry = context.getExtensionRegistry();
ExtensionContext extensionContext = context.getExtensionContext();
ThrowableCollector throwableCollector = context.getThrowableCollector();
for (T callback : registry.getExtensions(type)) {
Executable executable = generator.apply(extensionContext, callback);
throwableCollector.execute(executable);
if (throwableCollector.isNotEmpty()) {
break;
}
}
}
Aggregations