Search in sources :

Example 31 with ExtensionContext

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

the class ScriptExecutionConditionTests method throwingEvaluatorIsCreatedWhenDefaultEvaluatorClassNameIsIllegal.

@Test
void throwingEvaluatorIsCreatedWhenDefaultEvaluatorClassNameIsIllegal() throws ReflectiveOperationException {
    String name = "illegal class name";
    ScriptExecutionCondition condition = new ScriptExecutionCondition(name);
    ExtensionContext context = Mockito.mock(ExtensionContext.class);
    AnnotatedElement element = SimpleTestCases.class.getDeclaredMethod("testIsEnabled");
    Mockito.when(context.getElement()).thenReturn(Optional.of(element));
    Exception e = assertThrows(Exception.class, () -> condition.evaluateExecutionCondition(context));
    // 
    assertThat(e).isInstanceOf(// 
    ExtensionConfigurationException.class).hasMessageStartingWith("Creating instance of class `" + name + "` failed");
}
Also used : ExtensionConfigurationException(org.junit.jupiter.api.extension.ExtensionConfigurationException) AnnotatedElement(java.lang.reflect.AnnotatedElement) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) JUnitException(org.junit.platform.commons.JUnitException) ExtensionConfigurationException(org.junit.jupiter.api.extension.ExtensionConfigurationException) Test(org.junit.jupiter.api.Test)

Example 32 with ExtensionContext

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

the class ScriptExecutionConditionTests method enabledDueToAnnotatedElementNotPresent.

@Test
void enabledDueToAnnotatedElementNotPresent() {
    ScriptExecutionCondition condition = new ScriptExecutionCondition();
    ExtensionContext context = Mockito.mock(ExtensionContext.class);
    ConditionEvaluationResult result = condition.evaluateExecutionCondition(context);
    assertFalse(result.isDisabled());
    assertThat(result.getReason()).contains("AnnotatedElement not present");
}
Also used : ConditionEvaluationResult(org.junit.jupiter.api.extension.ConditionEvaluationResult) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Test(org.junit.jupiter.api.Test)

Example 33 with ExtensionContext

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

Example 34 with ExtensionContext

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

the class MethodArgumentsProvider method provideArguments.

@Override
public Stream<Arguments> provideArguments(ExtensionContext context) {
    Class<?> testClass = context.getRequiredTestClass();
    Object testInstance = context.getTestInstance().orElse(null);
    // @formatter:off
    return Arrays.stream(this.methodNames).map(methodName -> StringUtils.isNotBlank(methodName) ? methodName : context.getRequiredTestMethod().getName()).map(methodName -> ReflectionUtils.findMethod(testClass, methodName).orElseThrow(() -> new JUnitException(format("Could not find factory method [%s] in class [%s]", methodName, testClass.getName())))).map(method -> ReflectionUtils.invokeMethod(method, testInstance)).flatMap(CollectionUtils::toStream).map(MethodArgumentsProvider::toArguments);
// @formatter:on
}
Also used : Arrays(java.util.Arrays) Stream(java.util.stream.Stream) CollectionUtils(org.junit.platform.commons.util.CollectionUtils) JUnitException(org.junit.platform.commons.JUnitException) StringUtils(org.junit.platform.commons.util.StringUtils) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) AnnotationConsumer(org.junit.jupiter.params.support.AnnotationConsumer) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) String.format(java.lang.String.format) JUnitException(org.junit.platform.commons.JUnitException)

Example 35 with ExtensionContext

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

the class CsvArgumentsProvider method provideArguments.

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
    CsvParserSettings settings = new CsvParserSettings();
    settings.getFormat().setDelimiter(delimiter);
    settings.getFormat().setLineSeparator(LINE_SEPARATOR);
    settings.getFormat().setQuote('\'');
    settings.getFormat().setQuoteEscape('\'');
    settings.setEmptyValue("");
    settings.setAutoConfigurationEnabled(false);
    CsvParser csvParser = new CsvParser(settings);
    AtomicLong index = new AtomicLong(0);
    // @formatter:off
    return Arrays.stream(lines).map(line -> Preconditions.notNull(csvParser.parseLine(line + LINE_SEPARATOR), () -> "Line at index " + index.get() + " contains invalid CSV: \"" + line + "\"")).peek(values -> index.incrementAndGet()).map(Arguments::of);
// @formatter:on
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) CsvParserSettings(com.univocity.parsers.csv.CsvParserSettings) Preconditions(org.junit.platform.commons.util.Preconditions) Arrays(java.util.Arrays) Stream(java.util.stream.Stream) CsvParser(com.univocity.parsers.csv.CsvParser) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) AnnotationConsumer(org.junit.jupiter.params.support.AnnotationConsumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) CsvParserSettings(com.univocity.parsers.csv.CsvParserSettings) CsvParser(com.univocity.parsers.csv.CsvParser)

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