类org.springframework.core.convert.ConverterNotFoundException源码实例Demo

下面列出了怎么用org.springframework.core.convert.ConverterNotFoundException的API类实例代码及写法,或者点击链接到github查看源代码。

@Test
public void scalarList() throws Exception {
	List<String> list = new ArrayList<>();
	list.add("9");
	list.add("37");
	TypeDescriptor sourceType = TypeDescriptor.forObject(list);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarListTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(list, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	List<Integer> result = (List<Integer>) conversionService.convert(list, sourceType, targetType);
	assertFalse(list.equals(result));
	assertEquals(9, result.get(0).intValue());
	assertEquals(37, result.get(1).intValue());
}
 
@Test
public void scalarMap() throws Exception {
	Map<String, String> map = new HashMap<>();
	map.put("1", "9");
	map.put("2", "37");
	TypeDescriptor sourceType = TypeDescriptor.forObject(map);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));

	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}

	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals((Integer) 9, result.get(1));
	assertEquals((Integer) 37, result.get(2));
}
 
@Test
public void scalarMapNotGenericSourceField() throws Exception {
	Map<String, String> map = new HashMap<>();
	map.put("1", "9");
	map.put("2", "37");
	TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("notGenericMapSource"));
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));

	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}

	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals((Integer) 9, result.get(1));
	assertEquals((Integer) 37, result.get(2));
}
 
@Test
public void collectionMap() throws Exception {
	Map<String, List<String>> map = new HashMap<>();
	map.put("1", Arrays.asList("9", "12"));
	map.put("2", Arrays.asList("37", "23"));
	TypeDescriptor sourceType = TypeDescriptor.forObject(map);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));

	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}

	conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals(Arrays.asList(9, 12), result.get(1));
	assertEquals(Arrays.asList(37, 23), result.get(2));
}
 
@Test
public void collectionMapSourceTarget() throws Exception {
	Map<String, List<String>> map = new HashMap<>();
	map.put("1", Arrays.asList("9", "12"));
	map.put("2", Arrays.asList("37", "23"));
	TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("sourceCollectionMapTarget"));
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));

	assertFalse(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
		fail("Should have failed");
	}
	catch (ConverterNotFoundException ex) {
		// expected
	}

	conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals(Arrays.asList(9, 12), result.get(1));
	assertEquals(Arrays.asList(37, 23), result.get(2));
}
 
@Test
public void adaptedCollectionTypesFromSameSourceType() throws Exception {
	conversionService.addConverter(new MyStringToStringCollectionConverter());

	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));

	try {
		conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection")));
		fail("Should have thrown ConverterNotFoundException");
	}
	catch (ConverterNotFoundException ex) {
		// expected
	}
}
 
@Test
public void testConversionService() {
    Collection<?> source = Arrays.asList(new Person("Name", 30));

    Assert.assertFalse(conversionService.canConvert(Person.class, String.class));
    Assert.assertTrue(conversionService.canConvert(source.getClass(), String.class));

    try {
        conversionService.convert(source, String.class);
    } catch (ConversionFailedException e) {
        // Expected as Person can't be converted to a string according to
        // Spring's FallbackObjectToStringConverter, see javadoc for:
        //
        //   org.springframework.core.convert.support.FallbackObjectToStringConverter
        //
        Assert.assertTrue(e.getCause() instanceof ConverterNotFoundException);
    }


    Assert.assertNull(converter.convertTo(String.class, source));
}
 
@Test
public void scalarList() throws Exception {
	List<String> list = new ArrayList<>();
	list.add("9");
	list.add("37");
	TypeDescriptor sourceType = TypeDescriptor.forObject(list);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarListTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(list, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	List<Integer> result = (List<Integer>) conversionService.convert(list, sourceType, targetType);
	assertFalse(list.equals(result));
	assertEquals(9, result.get(0).intValue());
	assertEquals(37, result.get(1).intValue());
}
 
@Test
public void scalarMap() throws Exception {
	Map<String, String> map = new HashMap<>();
	map.put("1", "9");
	map.put("2", "37");
	TypeDescriptor sourceType = TypeDescriptor.forObject(map);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));

	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}

	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals((Integer) 9, result.get(1));
	assertEquals((Integer) 37, result.get(2));
}
 
@Test
public void scalarMapNotGenericSourceField() throws Exception {
	Map<String, String> map = new HashMap<>();
	map.put("1", "9");
	map.put("2", "37");
	TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("notGenericMapSource"));
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));

	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}

	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals((Integer) 9, result.get(1));
	assertEquals((Integer) 37, result.get(2));
}
 
@Test
public void collectionMap() throws Exception {
	Map<String, List<String>> map = new HashMap<>();
	map.put("1", Arrays.asList("9", "12"));
	map.put("2", Arrays.asList("37", "23"));
	TypeDescriptor sourceType = TypeDescriptor.forObject(map);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));

	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}

	conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals(Arrays.asList(9, 12), result.get(1));
	assertEquals(Arrays.asList(37, 23), result.get(2));
}
 
@Test
public void collectionMapSourceTarget() throws Exception {
	Map<String, List<String>> map = new HashMap<>();
	map.put("1", Arrays.asList("9", "12"));
	map.put("2", Arrays.asList("37", "23"));
	TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("sourceCollectionMapTarget"));
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));

	assertFalse(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
		fail("Should have failed");
	}
	catch (ConverterNotFoundException ex) {
		// expected
	}

	conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals(Arrays.asList(9, 12), result.get(1));
	assertEquals(Arrays.asList(37, 23), result.get(2));
}
 
@Test
public void adaptedCollectionTypesFromSameSourceType() throws Exception {
	conversionService.addConverter(new MyStringToStringCollectionConverter());

	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));

	try {
		conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection")));
		fail("Should have thrown ConverterNotFoundException");
	}
	catch (ConverterNotFoundException ex) {
		// expected
	}
}
 
源代码14 项目: spring-cloud-gray   文件: Binder.java
private <T> Object bindObject(ConfigurationPropertyName name, Bindable<T> target,
                              BindHandler handler, Context context, boolean allowRecursiveBinding) {
    ConfigurationProperty property = findProperty(name, context);
    if (property == null && containsNoDescendantOf(context.getSources(), name)) {
        return null;
    }
    AggregateBinder<?> aggregateBinder = getAggregateBinder(target, context);
    if (aggregateBinder != null) {
        return bindAggregate(name, target, handler, context, aggregateBinder);
    }
    if (property != null) {
        try {
            return bindProperty(target, context, property);
        } catch (ConverterNotFoundException ex) {
            // We might still be able to bind it as a bean
            Object bean = bindBean(name, target, handler, context,
                    allowRecursiveBinding);
            if (bean != null) {
                return bean;
            }
            throw ex;
        }
    }
    return bindBean(name, target, handler, context, allowRecursiveBinding);
}
 
@Test
public void scalarList() throws Exception {
	List<String> list = new ArrayList<String>();
	list.add("9");
	list.add("37");
	TypeDescriptor sourceType = TypeDescriptor.forObject(list);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarListTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(list, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	List<String> result = (List<String>) conversionService.convert(list, sourceType, targetType);
	assertFalse(list.equals(result));
	assertEquals(9, result.get(0));
	assertEquals(37, result.get(1));
}
 
@Test
public void scalarMap() throws Exception {
	Map<String, String> map = new HashMap<String, String>();
	map.put("1", "9");
	map.put("2", "37");
	TypeDescriptor sourceType = TypeDescriptor.forObject(map);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	} catch (ConversionFailedException e) {
		assertTrue(e.getCause() instanceof ConverterNotFoundException);
	}
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals((Integer) 9, result.get(1));
	assertEquals((Integer) 37, result.get(2));
}
 
@Test
public void scalarMapNotGenericSourceField() throws Exception {
	Map<String, String> map = new HashMap<String, String>();
	map.put("1", "9");
	map.put("2", "37");
	TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("notGenericMapSource"));
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	} catch (ConversionFailedException e) {
		assertTrue(e.getCause() instanceof ConverterNotFoundException);
	}
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals((Integer) 9, result.get(1));
	assertEquals((Integer) 37, result.get(2));
}
 
@Test
public void collectionMap() throws Exception {
	Map<String, List<String>> map = new HashMap<String, List<String>>();
	map.put("1", Arrays.asList("9", "12"));
	map.put("2", Arrays.asList("37", "23"));
	TypeDescriptor sourceType = TypeDescriptor.forObject(map);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	} catch (ConversionFailedException e) {
		assertTrue(e.getCause() instanceof ConverterNotFoundException);
	}
	conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals(Arrays.asList(9, 12), result.get(1));
	assertEquals(Arrays.asList(37, 23), result.get(2));
}
 
@Test
public void collectionMapSourceTarget() throws Exception {
	Map<String, List<String>> map = new HashMap<String, List<String>>();
	map.put("1", Arrays.asList("9", "12"));
	map.put("2", Arrays.asList("37", "23"));
	TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("sourceCollectionMapTarget"));
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));
	assertFalse(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
		fail("Should have failed");
	}
	catch (ConverterNotFoundException ex) {
		// expected
	}
	conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals(Arrays.asList(9, 12), result.get(1));
	assertEquals(Arrays.asList(37, 23), result.get(2));
}
 
@Test
public void adaptedCollectionTypesFromSameSourceType() throws Exception {
	conversionService.addConverter(new MyStringToStringCollectionConverter());

	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));

	try {
		conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection")));
		fail("Should have thrown ConverterNotFoundException");
	}
	catch (ConverterNotFoundException ex) {
		// expected
	}
}
 
@Test
public void testAggregateDailyStatistics(){
   try {
      DailyStatistic stat = repository.aggregateDailyStatistics("20140930");
   } catch (ConverterNotFoundException cvnfe){
      // For now, mapReduce in Fongo is experimental. MapReduce execution is working
      // but SpringData cannot convert Fongo Rhino result into Java object
      // ("No converter found capable of converting from type org.mozilla.javascript.UniqueTag to type java.lang.Integer")
   } catch (UncategorizedMongoDbException ume){
      // For now, mapReduce in Fongo is experimental. MapReduce execution is not working
      // ("org.mozilla.javascript.EcmaError: TypeError: Cannot read property "0" from undefined")
   } catch (RuntimeException re){
      // For now, mapReduce in Fongo is experimental. MapReduce execution is working
      // but SpringData cannot convert Fongo Rhino result into Java object
      // ("json can't serialize type : class org.mozilla.javascript.UniqueTag")
   }
}
 
源代码22 项目: sdn-rx   文件: ReactiveRepositoryIT.java
@Test
void findByPropertyFailsIfNoConverterIsAvailable(@Autowired ReactivePersonRepository repository) {

	assertThatExceptionOfType(ConverterNotFoundException.class)
		.isThrownBy(() -> repository.findAllByPlace(new ThingWithGeneratedId("hello")))
		.withMessageStartingWith("No converter found capable of converting from type");
}
 
源代码23 项目: sdn-rx   文件: DefaultNeo4jConverterTest.java
@Test
void shouldCatchUncoerfcibleErrors() {
	Value value = Values.value("Das funktioniert nicht.");

	assertThatExceptionOfType(TypeMismatchDataAccessException.class)
		.isThrownBy(
			() -> defaultNeo4jConverter.readValueForProperty(value, ClassTypeInformation.from(ReactiveNeo4jClient.class)))
		.withMessageStartingWith(
			"Could not convert \"Das funktioniert nicht.\" into org.neo4j.springframework.data.core.ReactiveNeo4jClient;")
		.withRootCauseInstanceOf(ConverterNotFoundException.class);
}
 
@Nullable
private Object handleConverterNotFound(
		@Nullable Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {

	if (source == null) {
		assertNotPrimitiveTargetType(sourceType, targetType);
		return null;
	}
	if ((sourceType == null || sourceType.isAssignableTo(targetType)) &&
			targetType.getObjectType().isInstance(source)) {
		return source;
	}
	throw new ConverterNotFoundException(sourceType, targetType);
}
 
@Test
public void getProperty_withNonConvertibleTargetType() {
	testProperties.put("foo", "bar");

	class TestType { }

	try {
		propertyResolver.getProperty("foo", TestType.class);
		fail("Expected ConverterNotFoundException due to non-convertible types");
	}
	catch (ConverterNotFoundException ex) {
		// expected
	}
}
 
@Test
public void convertListOfNonStringifiable() {
	List<Object> list = Arrays.asList(new TestEntity(1L), new TestEntity(2L));
	assertTrue(conversionService.canConvert(list.getClass(), String.class));
	try {
		conversionService.convert(list, String.class);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getMessage().contains(list.getClass().getName()));
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
		assertTrue(ex.getCause().getMessage().contains(TestEntity.class.getName()));
	}
}
 
@Test(expected = ConverterNotFoundException.class)
public void elementTypesNotConvertible() throws Exception {
	List<String> resources = new ArrayList<>();
	resources.add(null);
	resources.add(null);
	TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("strings"));
	assertEquals(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources"))));
}
 
@Test
public void convertFromStreamToArrayNoConverter() throws NoSuchFieldException {
	Stream<Integer> stream = Arrays.asList(1, 2, 3).stream();
	TypeDescriptor arrayOfLongs = new TypeDescriptor(Types.class.getField("arrayOfLongs"));
	assertThatExceptionOfType(ConversionFailedException.class).isThrownBy(() ->
			this.conversionService.convert(stream, arrayOfLongs))
		.withCauseInstanceOf(ConverterNotFoundException.class);
}
 
@Test
public void encodeToTextCannotConvert() throws Exception {
	setup(NoConvertersConfig.class);
	assertThatExceptionOfType(EncodeException.class).isThrownBy(() ->
			new MyTextEncoder().encode(myType))
		.withCauseInstanceOf(ConverterNotFoundException.class);
}
 
@Test
public void encodeToBinaryCannotConvert() throws Exception {
	setup(NoConvertersConfig.class);
	assertThatExceptionOfType(EncodeException.class).isThrownBy(() ->
			new MyBinaryEncoder().encode(myType))
		.withCauseInstanceOf(ConverterNotFoundException.class);
}
 
 类所在包
 同包方法