Java 类org.junit.jupiter.params.converter.ArgumentConversionException 实例源码

项目:junit5-extensions    文件:ClassArgumentConverter.java   
@VisibleForTesting
Class<?> convert(String input, TypeToken<? extends Class<?>> targetType)
    throws ArgumentConversionException {
  Class<?> inputType;
  try {
    inputType = Class.forName(input);
  } catch (ClassNotFoundException e) {
    throw new ArgumentConversionException("Could not convert: " + input, e);
  }

  TypeToken<? extends Class<?>> inputClassType = asClassType(inputType);

  if (!targetType.isSupertypeOf(inputClassType)) {
    throw new ArgumentConversionException(
        String.format("%s is not assignable to %s", inputClassType, targetType));
  }

  return inputType;
}
项目:junit5-extensions    文件:ClassArgumentConverter.java   
@SuppressWarnings("unchecked")
@Override
public Object convert(Object input, ParameterContext context) throws ArgumentConversionException {
  TypeToken<?> parameterType = TypeToken.of(context.getParameter().getParameterizedType());
  if (parameterType.getRawType() != Class.class) {
    throw new ArgumentConversionException(
        String.format("Could not convert: %s. Invalid parameter type.", input));
  }

  return convert(input.toString(), (TypeToken<? extends Class<?>>) parameterType);
}
项目: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) {}
项目:junit5-extensions    文件:ClassArgumentConverterTest.java   
@Test
void classDoesNotExist() {
  ArgumentConversionException e = assertThrows(ArgumentConversionException.class,
      () -> converter.convert("doesnotexist", THROWABLE));
  assertThat(e).hasMessageThat().contains("Could not convert: doesnotexist");
  assertThat(e).hasCauseThat().isInstanceOf(ClassNotFoundException.class);
}
项目:demo-junit-5    文件:CustomArgumentConverterTest.java   
@Override
public Object convert(Object input, ParameterContext parameterContext) throws ArgumentConversionException {
    if (input instanceof Point)
        return input;
    if (input instanceof String)
        try {
            return Point.from((String) input);
        } catch (NumberFormatException ex) {
            String message = input + " is no correct string representation of a point.";
            throw new ArgumentConversionException(message, ex);
        }
    throw new ArgumentConversionException(input + " is no valid point");
}
项目:keystore-explorer    文件:JavaVersionTest.java   
@Override
public Object convert(Object source, ParameterContext context) throws ArgumentConversionException {
    try {
        return JavaVersion.class.getField(String.valueOf(source)).get(null);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        throw new ArgumentConversionException(e.toString());
    }
}
项目:junit5-extensions    文件:ClassArgumentConverterTest.java   
void testIncompatible(String input, TypeToken<? extends Class<?>> type) {
  ArgumentConversionException e = assertThrows(ArgumentConversionException.class,
      () -> converter.convert(input, type));
  assertThat(e).hasMessageThat().contains("is not assignable to");
}