use of org.junit.jupiter.api.extension.ExtensionContext in project junit5 by junit-team.
the class ScriptExecutionConditionTests method enabledDueToAnnotationNotPresent.
@Test
void enabledDueToAnnotationNotPresent() {
ScriptExecutionCondition condition = new ScriptExecutionCondition();
ExtensionContext context = Mockito.mock(ExtensionContext.class);
Optional<AnnotatedElement> optionalElement = Optional.of(ScriptExecutionConditionTests.class);
Mockito.when(context.getElement()).thenReturn(optionalElement);
ConditionEvaluationResult result = condition.evaluateExecutionCondition(context);
assertFalse(result.isDisabled());
assertThat(result.getReason()).contains("Annotation not present");
}
use of org.junit.jupiter.api.extension.ExtensionContext in project junit5 by junit-team.
the class ExtensionContextTests method configurationParameter.
@TestFactory
Stream<DynamicTest> configurationParameter() throws Exception {
ConfigurationParameters echo = new EchoParameters();
String key = "123";
Optional<String> expected = Optional.of(key);
UniqueId engineUniqueId = UniqueId.parse("[engine:junit-jupiter]");
JupiterEngineDescriptor engineDescriptor = new JupiterEngineDescriptor(engineUniqueId);
UniqueId classUniqueId = UniqueId.parse("[engine:junit-jupiter]/[class:MyClass]");
ClassTestDescriptor classTestDescriptor = new ClassTestDescriptor(classUniqueId, getClass());
Method method = getClass().getDeclaredMethod("configurationParameter");
UniqueId methodUniqueId = UniqueId.parse("[engine:junit-jupiter]/[class:MyClass]/[method:myMethod]");
TestMethodTestDescriptor methodTestDescriptor = new TestMethodTestDescriptor(methodUniqueId, getClass(), method);
return //
Stream.of(//
(ExtensionContext) new JupiterEngineExtensionContext(null, engineDescriptor, echo), //
new ClassExtensionContext(null, null, classTestDescriptor, echo, null), //
new MethodExtensionContext(null, null, methodTestDescriptor, echo, null, null)).map(context -> dynamicTest(context.getClass().getSimpleName(), () -> assertEquals(expected, context.getConfigurationParameter(key))));
}
use of org.junit.jupiter.api.extension.ExtensionContext 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.ExtensionContext in project junit5 by junit-team.
the class TestFactoryTestDescriptorTests method before.
@BeforeEach
void before() throws Exception {
extensionContext = mock(ExtensionContext.class);
isClosed = false;
context = new JupiterEngineExecutionContext(null, null).extend().withThrowableCollector(new ThrowableCollector()).withExtensionContext(extensionContext).build();
Method testMethod = CustomStreamTestCase.class.getDeclaredMethod("customStream");
descriptor = new TestFactoryTestDescriptor(UniqueId.forEngine("engine"), CustomStreamTestCase.class, testMethod);
when(extensionContext.getTestMethod()).thenReturn(Optional.of(testMethod));
}
use of org.junit.jupiter.api.extension.ExtensionContext in project junit5 by junit-team.
the class TestMethodTestDescriptor method invokeAllAfterMethodsOrCallbacks.
private <T extends Extension> void invokeAllAfterMethodsOrCallbacks(JupiterEngineExecutionContext context, BiFunction<ExtensionContext, T, Executable> generator, Class<T> type) {
ExtensionRegistry registry = context.getExtensionRegistry();
ExtensionContext extensionContext = context.getExtensionContext();
ThrowableCollector throwableCollector = context.getThrowableCollector();
registry.getReversedExtensions(type).forEach(callback -> {
Executable executable = generator.apply(extensionContext, callback);
throwableCollector.execute(executable);
});
}
Aggregations