### Question:
ValueConverter { public <R> R convert( @Nullable Object value, @Nonnull Class<R> targetType ) { try { return conversionService.convert( value, targetType ); } catch ( org.springframework.core.convert.ConversionException e ) { throw new ConversionException( e.getMessage() ); } } ValueConverter( @Nonnull ObjectProvider<List<TypedConverter<?, ?>>> converters ); R convert( @Nullable Object value, @Nonnull Class<R> targetType ); @SuppressWarnings( "unchecked" ) @Nullable R convert( @Nullable Object value, @Nonnull ValueType valueType, @Nonnull Class<R> resultClass );  }### Answer:
@Test public void testDifferentType() { final StringToIntegerConverter converter1 = new StringToIntegerConverter(); final StringToZonedDateTimeConverter converter2 = new StringToZonedDateTimeConverter(); final ValueConverter valueConverter = new ValueConverter( new StaticObjectProvider<>( Arrays.asList( converter1, converter2 ) ) ); Assert.assertNotNull( valueConverter.convert( "2017-08-04T17:28:43Z", ValueType.DATETIME, ZonedDateTime.class ) ); Assert.assertEquals( (Integer) 12, valueConverter.convert( "12", ValueType.INTEGER_POSITIVE, Integer.class ) ); }
@Test public void testNull() { final StringToIntegerConverter converter1 = new StringToIntegerConverter(); final ValueConverter valueConverter = new ValueConverter( new StaticObjectProvider<>( Collections.singletonList( converter1 ) ) ); Assert.assertNull( valueConverter.convert( null, ValueType.INTEGER_POSITIVE, Integer.class ) ); }
@Test public void testSameClass() { final StringToIntegerConverter converter1 = new StringToIntegerConverter(); final ValueConverter valueConverter = new ValueConverter( new StaticObjectProvider<>( Collections.singletonList( converter1 ) ) ); Assert.assertEquals( "Test", valueConverter.convert( "Test", ValueType.INTEGER_POSITIVE, String.class ) ); }
@Test( expected = ConversionException.class ) public void testNoConverter() { final StringToIntegerConverter converter1 = new StringToIntegerConverter(); final ValueConverter valueConverter = new ValueConverter( new StaticObjectProvider<>( Collections.singletonList( converter1 ) ) ); valueConverter.convert( new Date(), ValueType.DATETIME, String.class ); }