ModelMapperConverterServiceImpl implements ConverterService { public void setupCustomConverters(List<ConverterAdapter> converterAdapters) { Assert.notNull(converterAdapters); for (ConverterAdapter c : converterAdapters) { modelMapper.createTypeMap(c.getSourceClass(), c.getTargetClass()) .setConverter(context -> c.getConverter().convert(context.getSource())); } } ModelMapperConverterServiceImpl(ModelMapper modelMapper); void setupCustomConverters(List<ConverterAdapter> converterAdapters); @Override T convert(S source, Class<T> targetType); @Override List<T> convert(List<S> sourceList, Class<T> targetType);  }
@Test(expected = IllegalArgumentException.class) public void setupCustomConvertersShouldThrowIllegalArgumentExceptionWhenConverterAdapterListIsNull() { modelMapperConverterService.setupCustomConverters(null); }
@Test public void setupCustomConvertersShouldAddCustomConvertersToModelMapperWhenGivenConverterAdapterListHasElements() { List<ConverterAdapter> twoElementConverterAdapterList = Arrays.asList( new ConverterAdapter(TestClass1.class, TestClass2.class, new CustomConverter<TestClass1, TestClass2>() { @Override public TestClass2 convert(TestClass1 source) { return new TestClass2(source.getId()); } }), new ConverterAdapter(TestClass2.class, TestClass1.class, new CustomConverter<TestClass2, TestClass1>() { @Override public TestClass1 convert(TestClass2 source) { return new TestClass1(source.getId()); } }) ); modelMapperConverterService.setupCustomConverters(twoElementConverterAdapterList); then(modelMapper).should().createTypeMap(TestClass1.class, TestClass2.class); then(modelMapper).should().createTypeMap(TestClass2.class, TestClass1.class); }
@Test public void setupCustomConvertersShouldNotAddCustomConvertersToModelMapperWhenGivenConverterAdapterListIsEmpty() { List<ConverterAdapter> emptyConverterAdapterList = Collections.emptyList(); modelMapperConverterService.setupCustomConverters(emptyConverterAdapterList); then(modelMapper).should(never()).createTypeMap(any(), any()); }