Search in sources :

Example 1 with ExtensionContext

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

the class ParameterizedTestExtensionTests method supportsReturnsFalseForMissingTestMethod.

@Test
void supportsReturnsFalseForMissingTestMethod() {
    ExtensionContext extensionContextWithoutTestMethod = getExtensionContextReturningSingleMethod(new TestCaseWithoutMethod());
    assertFalse(this.parameterizedTestExtension.supportsTestTemplate(extensionContextWithoutTestMethod));
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Test(org.junit.jupiter.api.Test)

Example 2 with ExtensionContext

use of org.junit.jupiter.api.extension.ExtensionContext 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 3 with ExtensionContext

use of org.junit.jupiter.api.extension.ExtensionContext 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 4 with ExtensionContext

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

the class CsvFileArgumentsProviderTests method provide.

private Stream<Object[]> provide(CsvFileArgumentsProvider provider, CsvFileSource annotation) {
    provider.accept(annotation);
    ExtensionContext context = mock(ExtensionContext.class);
    when(context.getTestClass()).thenReturn(Optional.of(CsvFileArgumentsProviderTests.class));
    doCallRealMethod().when(context).getRequiredTestClass();
    return provider.provideArguments(context).map(Arguments::get);
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext)

Example 5 with ExtensionContext

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

the class MethodArgumentsProviderTests method provideArguments.

private Stream<Object[]> provideArguments(Class<?> testClass, Method testMethod, boolean allowNonStaticMethod, String... methodNames) {
    MethodSource annotation = mock(MethodSource.class);
    when(annotation.value()).thenReturn(methodNames);
    ExtensionContext context = mock(ExtensionContext.class);
    when(context.getTestClass()).thenReturn(Optional.ofNullable(testClass));
    when(context.getTestMethod()).thenReturn(Optional.ofNullable(testMethod));
    doCallRealMethod().when(context).getRequiredTestMethod();
    doCallRealMethod().when(context).getRequiredTestClass();
    Object testInstance = allowNonStaticMethod ? ReflectionUtils.newInstance(testClass) : null;
    when(context.getTestInstance()).thenReturn(Optional.ofNullable(testInstance));
    MethodArgumentsProvider provider = new MethodArgumentsProvider();
    provider.accept(annotation);
    return provider.provideArguments(context).map(Arguments::get);
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext)

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