Search in sources :

Example 6 with ExtensionContext

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");
}
Also used : ConditionEvaluationResult(org.junit.jupiter.api.extension.ConditionEvaluationResult) AnnotatedElement(java.lang.reflect.AnnotatedElement) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Test(org.junit.jupiter.api.Test)

Example 7 with ExtensionContext

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))));
}
Also used : UniqueId(org.junit.platform.engine.UniqueId) Method(java.lang.reflect.Method) ConfigurationParameters(org.junit.platform.engine.ConfigurationParameters) NestedClassTestDescriptor(org.junit.jupiter.engine.descriptor.NestedClassTestDescriptor) ClassTestDescriptor(org.junit.jupiter.engine.descriptor.ClassTestDescriptor) JupiterEngineDescriptor(org.junit.jupiter.engine.descriptor.JupiterEngineDescriptor) MethodExtensionContext(org.junit.jupiter.engine.descriptor.MethodExtensionContext) ClassExtensionContext(org.junit.jupiter.engine.descriptor.ClassExtensionContext) MethodExtensionContext(org.junit.jupiter.engine.descriptor.MethodExtensionContext) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) JupiterEngineExtensionContext(org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext) ClassExtensionContext(org.junit.jupiter.engine.descriptor.ClassExtensionContext) JupiterEngineExtensionContext(org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext) TestMethodTestDescriptor(org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor) TestFactory(org.junit.jupiter.api.TestFactory)

Example 8 with ExtensionContext

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);
    }
}
Also used : AnnotationConsumerInitializer(org.junit.jupiter.params.support.AnnotationConsumerInitializer) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) ParameterContext(org.junit.jupiter.api.extension.ParameterContext) ConvertWith(org.junit.jupiter.params.converter.ConvertWith) Parameter(java.lang.reflect.Parameter) Executable(java.lang.reflect.Executable) Optional(java.util.Optional) AnnotationUtils(org.junit.platform.commons.util.AnnotationUtils) Method(java.lang.reflect.Method) ParameterResolver(org.junit.jupiter.api.extension.ParameterResolver) ArgumentConverter(org.junit.jupiter.params.converter.ArgumentConverter) DefaultArgumentConverter(org.junit.jupiter.params.converter.DefaultArgumentConverter) ArgumentConverter(org.junit.jupiter.params.converter.ArgumentConverter) DefaultArgumentConverter(org.junit.jupiter.params.converter.DefaultArgumentConverter) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) ConvertWith(org.junit.jupiter.params.converter.ConvertWith) Parameter(java.lang.reflect.Parameter) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException)

Example 9 with ExtensionContext

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));
}
Also used : JupiterEngineExecutionContext(org.junit.jupiter.engine.execution.JupiterEngineExecutionContext) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Method(java.lang.reflect.Method) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 10 with ExtensionContext

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

Aggregations

ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)37 ExtensionRegistry (org.junit.jupiter.engine.extension.ExtensionRegistry)13 Method (java.lang.reflect.Method)11 ThrowableCollector (org.junit.jupiter.engine.execution.ThrowableCollector)11 Test (org.junit.jupiter.api.Test)10 Optional (java.util.Optional)7 UniqueId (org.junit.platform.engine.UniqueId)5 List (java.util.List)4 Stream (java.util.stream.Stream)4 API (org.apiguardian.api.API)4 INTERNAL (org.apiguardian.api.API.Status.INTERNAL)4 Executable (org.junit.jupiter.api.function.Executable)4 JupiterEngineExecutionContext (org.junit.jupiter.engine.execution.JupiterEngineExecutionContext)4 Preconditions (org.junit.platform.commons.util.Preconditions)4 Arrays (java.util.Arrays)3 AfterEachCallback (org.junit.jupiter.api.extension.AfterEachCallback)3 BeforeEachCallback (org.junit.jupiter.api.extension.BeforeEachCallback)3 TestTemplateInvocationContext (org.junit.jupiter.api.extension.TestTemplateInvocationContext)3 ClassExtensionContext (org.junit.jupiter.engine.descriptor.ClassExtensionContext)3 JUnitException (org.junit.platform.commons.JUnitException)3