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");
}
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");
}
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
}
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
}
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
}
Aggregations