Search in sources :

Example 1 with TestTemplateInvocationContext

use of org.junit.jupiter.api.extension.TestTemplateInvocationContext in project junit5 by junit-team.

the class ParameterizedTestExtensionTests method streamsReturnedByProvidersAreClosedWhenCallingProvide.

@Test
void streamsReturnedByProvidersAreClosedWhenCallingProvide() {
    ExtensionContext extensionContext = getExtensionContextReturningSingleMethod(new TestCaseWithArgumentSourceAnnotatedMethod());
    Stream<TestTemplateInvocationContext> stream = this.parameterizedTestExtension.provideTestTemplateInvocationContexts(extensionContext);
    // cause the stream to be evaluated
    stream.count();
    assertTrue(streamWasClosed);
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) TestTemplateInvocationContext(org.junit.jupiter.api.extension.TestTemplateInvocationContext) Test(org.junit.jupiter.api.Test)

Example 2 with TestTemplateInvocationContext

use of org.junit.jupiter.api.extension.TestTemplateInvocationContext in project junit5 by junit-team.

the class ParameterizedTestExtensionTests method throwsExceptionWhenParameterizedTestIsNotInvokedAtLeastOnce.

@Test
void throwsExceptionWhenParameterizedTestIsNotInvokedAtLeastOnce() {
    ExtensionContext extensionContextWithAnnotatedTestMethod = getExtensionContextReturningSingleMethod(new TestCaseWithAnnotatedMethod());
    Stream<TestTemplateInvocationContext> stream = this.parameterizedTestExtension.provideTestTemplateInvocationContexts(extensionContextWithAnnotatedTestMethod);
    // cause the stream to be evaluated
    stream.toArray();
    JUnitException exception = assertThrows(JUnitException.class, stream::close);
    assertThat(exception).hasMessage("Configuration error: You must provide at least one argument for this @ParameterizedTest");
}
Also used : JUnitException(org.junit.platform.commons.JUnitException) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) TestTemplateInvocationContext(org.junit.jupiter.api.extension.TestTemplateInvocationContext) Test(org.junit.jupiter.api.Test)

Example 3 with TestTemplateInvocationContext

use of org.junit.jupiter.api.extension.TestTemplateInvocationContext in project junit5 by junit-team.

the class ParameterizedTestExtension method provideTestTemplateInvocationContexts.

@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) {
    Method templateMethod = context.getRequiredTestMethod();
    ParameterizedTestNameFormatter formatter = createNameFormatter(templateMethod);
    AtomicLong invocationCount = new AtomicLong(0);
    // @formatter:off
    return findRepeatableAnnotations(templateMethod, ArgumentsSource.class).stream().map(ArgumentsSource::value).map(ReflectionUtils::newInstance).map(provider -> AnnotationConsumerInitializer.initialize(templateMethod, provider)).flatMap(provider -> arguments(provider, context)).map(Arguments::get).map(arguments -> consumedArguments(arguments, templateMethod)).map(arguments -> createInvocationContext(formatter, arguments)).peek(invocationContext -> invocationCount.incrementAndGet()).onClose(() -> Preconditions.condition(invocationCount.get() > 0, () -> "Configuration error: You must provide at least one argument for this @" + ParameterizedTest.class.getSimpleName()));
// @formatter:on
}
Also used : AnnotationConsumerInitializer(org.junit.jupiter.params.support.AnnotationConsumerInitializer) Preconditions(org.junit.platform.commons.util.Preconditions) Arrays(java.util.Arrays) AnnotationUtils.isAnnotated(org.junit.platform.commons.util.AnnotationUtils.isAnnotated) AnnotationUtils.findAnnotation(org.junit.platform.commons.util.AnnotationUtils.findAnnotation) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Arguments(org.junit.jupiter.params.provider.Arguments) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) AtomicLong(java.util.concurrent.atomic.AtomicLong) Stream(java.util.stream.Stream) TestTemplateInvocationContext(org.junit.jupiter.api.extension.TestTemplateInvocationContext) ExceptionUtils(org.junit.platform.commons.util.ExceptionUtils) TestTemplateInvocationContextProvider(org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider) AnnotationUtils.findRepeatableAnnotations(org.junit.platform.commons.util.AnnotationUtils.findRepeatableAnnotations) ArgumentsProvider(org.junit.jupiter.params.provider.ArgumentsProvider) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource) Method(java.lang.reflect.Method) AtomicLong(java.util.concurrent.atomic.AtomicLong) Arguments(org.junit.jupiter.params.provider.Arguments) Method(java.lang.reflect.Method) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource)

Aggregations

ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)3 TestTemplateInvocationContext (org.junit.jupiter.api.extension.TestTemplateInvocationContext)3 Test (org.junit.jupiter.api.Test)2 Method (java.lang.reflect.Method)1 Arrays (java.util.Arrays)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Stream (java.util.stream.Stream)1 TestTemplateInvocationContextProvider (org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider)1 Arguments (org.junit.jupiter.params.provider.Arguments)1 ArgumentsProvider (org.junit.jupiter.params.provider.ArgumentsProvider)1 ArgumentsSource (org.junit.jupiter.params.provider.ArgumentsSource)1 AnnotationConsumerInitializer (org.junit.jupiter.params.support.AnnotationConsumerInitializer)1 JUnitException (org.junit.platform.commons.JUnitException)1 AnnotationUtils.findAnnotation (org.junit.platform.commons.util.AnnotationUtils.findAnnotation)1 AnnotationUtils.findRepeatableAnnotations (org.junit.platform.commons.util.AnnotationUtils.findRepeatableAnnotations)1 AnnotationUtils.isAnnotated (org.junit.platform.commons.util.AnnotationUtils.isAnnotated)1 ExceptionUtils (org.junit.platform.commons.util.ExceptionUtils)1 Preconditions (org.junit.platform.commons.util.Preconditions)1 ReflectionUtils (org.junit.platform.commons.util.ReflectionUtils)1