Java 类org.junit.jupiter.params.converter.ConvertWith 实例源码
项目:autotest
文件:AutoTestParameterResolver.java
@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("参数转换出错:" + parameter.getType().getName(),
ex);
}
}
项目:keystore-explorer
文件:JavaVersionTest.java
@ParameterizedTest
@CsvSource({
"JRE_VERSION_9, JRE_VERSION_160, true",
"JRE_VERSION_9, JRE_VERSION_170, true",
"JRE_VERSION_9, JRE_VERSION_180, true",
"JRE_VERSION_9, JRE_VERSION_9, true",
"JRE_VERSION_180, JRE_VERSION_160, true",
"JRE_VERSION_180, JRE_VERSION_170, true",
"JRE_VERSION_180, JRE_VERSION_180, true",
"JRE_VERSION_170, JRE_VERSION_160, true",
"JRE_VERSION_170, JRE_VERSION_170, true",
"JRE_VERSION_180, JRE_VERSION_9, false",
"JRE_VERSION_170, JRE_VERSION_180, false",
"JRE_VERSION_170, JRE_VERSION_9, false",
})
public void isAtLeast(@ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion version1,
@ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion version2, boolean result) {
assertEquals(version1.isAtLeast(version2), result);
}
项目:keystore-explorer
文件:JavaVersionTest.java
@ParameterizedTest
@CsvSource({
"JRE_VERSION_160, JRE_VERSION_170, true",
"JRE_VERSION_170, JRE_VERSION_180, true",
"JRE_VERSION_180, JRE_VERSION_9, true",
"JRE_VERSION_9, JRE_VERSION_180, false",
"JRE_VERSION_170, JRE_VERSION_160, false",
"JRE_VERSION_170, JRE_VERSION_170, false",
"JRE_VERSION_180, JRE_VERSION_160, false",
"JRE_VERSION_180, JRE_VERSION_170, false",
"JRE_VERSION_180, JRE_VERSION_180, false",
})
public void isBelow(@ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion version1,
@ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion version2, boolean result) {
assertEquals(version1.isBelow(version2), result);
}
项目:mastering-junit5
文件:ExplicitConversionParameterizedTest.java
@ParameterizedTest
@EnumSource(TimeUnit.class)
void testWithExplicitArgumentConversion(
@ConvertWith(CustomArgumentsConverter.class) String argument) {
System.out.println("Argument " + argument + " is a type of "
+ argument.getClass());
assertNotNull(argument);
}
项目:junit5-extensions
文件:UsageExampleTests.java
@ParameterizedTest
@ValueSource(strings = {
"java.util.List",
"java.util.Collection",
"java.lang.Object",
})
void anySuperclass(
@ConvertWith(ClassArgumentConverter.class) Class<? super List<?>> superclassOfList) {
assertThat(superclassOfList).isNotNull();
}
项目:junit5-extensions
文件:UsageExampleTests.java
@ParameterizedTest
@ValueSource(strings = {
"java.util.List",
"java.util.ArrayList",
})
void anySubclass(@ConvertWith(ClassArgumentConverter.class) Class<? extends List<?>> anything) {
assertThat(anything).isNotNull();
}
项目:junit5-extensions
文件:UsageExampleTests.java
@ExpectFailure({
@Cause(type = ParameterResolutionException.class),
@Cause(type = ArgumentConversionException.class),
@Cause(type = ClassNotFoundException.class)
})
@ParameterizedTest
@ValueSource(strings = "123ClassDoesNotExist")
void classNotFound(@ConvertWith(ClassArgumentConverter.class) Class<?> clazz) {}
项目:junit5-extensions
文件:UsageExampleTests.java
@ExpectFailure({
@Cause(type = ParameterResolutionException.class),
@Cause(type = ArgumentConversionException.class, message = "Invalid parameter type")
})
@ParameterizedTest
@ValueSource(strings = "java.lang.Object")
void badParameterType(@ConvertWith(ClassArgumentConverter.class) String clazz) {}
项目:junit5-extensions
文件:UsageExampleTests.java
@ExpectFailure({
@Cause(type = ParameterResolutionException.class),
@Cause(
type = ArgumentConversionException.class,
message = "java.lang.Class<java.util.List> is not assignable to"
+ " java.lang.Class<java.util.Collection<?>>"
)
})
@ParameterizedTest
@ValueSource(strings = "java.util.List")
void wrongClass(@ConvertWith(ClassArgumentConverter.class) Class<Collection<?>> clazz) {}
项目:junit5-extensions
文件:UsageExampleTests.java
@ExpectFailure({
@Cause(type = ParameterResolutionException.class),
@Cause(type = ArgumentConversionException.class, message = "is not assignable to")
})
@ParameterizedTest
@ValueSource(strings = "java.util.List")
void badLowerBound(
@ConvertWith(ClassArgumentConverter.class) Class<? super Collection<?>> clazz) {}
项目:junit5-extensions
文件:UsageExampleTests.java
@ExpectFailure({
@Cause(type = ParameterResolutionException.class),
@Cause(type = ArgumentConversionException.class, message = "is not assignable to")
})
@ParameterizedTest
@ValueSource(strings = "java.lang.Object")
void badUpperBound(
@ConvertWith(ClassArgumentConverter.class) Class<? extends Collection<?>> clazz) {}
项目:Mastering-Software-Testing-with-JUnit-5
文件:ExplicitConversionParameterizedTest.java
@ParameterizedTest
@EnumSource(TimeUnit.class)
void testWithExplicitArgumentConversion(
@ConvertWith(CustomArgumentsConverter.class) String argument) {
System.out.println("Argument " + argument + " is a type of "
+ argument.getClass());
assertNotNull(argument);
}
项目:keystore-explorer
文件:JavaVersionTest.java
@ParameterizedTest
@CsvSource({
"JRE_VERSION_160, 1.6.0",
"JRE_VERSION_170, 1.7.0",
"JRE_VERSION_180, 1.8.0",
})
public void equals(@ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion verConst, String verStr) {
assertEquals(verConst, new JavaVersion(verStr));
}
项目:keystore-explorer
文件:JavaVersionTest.java
@ParameterizedTest
@CsvSource({
"JRE_VERSION_160, 1.6.0_45, 0",
"JRE_VERSION_170, 1.6.0_45, 1",
"JRE_VERSION_170, 1.7.0_101, 0",
"JRE_VERSION_180, 1.7.0_101, 1",
"JRE_VERSION_180, 1.8.0_20, 0",
"JRE_VERSION_170, 1.8.0_20, -1",
"JRE_VERSION_160, 1.8.0_20, -1",
})
public void compareTo(@ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion verConst, String verStr,
int result) {
assertEquals(Integer.signum(verConst.compareTo(new JavaVersion(verStr))), result);
}
项目:demo-junit-5
文件:CustomArgumentConverterTest.java
@ParameterizedTest
@ValueSource(strings = { "(0/0)", "(0/1)", "(1/0)", "(1/1)", "(2/1)" })
void testPointNotNull_1(@ConvertWith(PointConverter.class) Point point) {
assertNotNull(point);
}