ModelMapperConverterServiceImpl implements ConverterService { @Override public <S, T> T convert(S source, Class<T> targetType) { Assert.notNull(source); Assert.notNull(targetType); return modelMapper.map(source, targetType); } 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 convertSingleElementShouldThrowIllegalArgumentExceptionWhenSourceParameterIsNull() { modelMapperConverterService.convert(null, TestClass2.class); }
@Test(expected = IllegalArgumentException.class) public void convertSingleElementShouldThrowIllegalArgumentExceptionWhenTargetTypeParameterIsNull() { TestClass1 source = new TestClass1(TEST_CLASS_FIRST_ID); modelMapperConverterService.convert(source, null); }
@Test public void convertSingleElementShouldReturnConvertedObjectWhenProperParametersWereGiven() { TestClass1 source = new TestClass1(TEST_CLASS_FIRST_ID); TestClass2 expected = new TestClass2(TEST_CLASS_FIRST_ID); given(modelMapper.map(source, TestClass2.class)).willReturn(expected); TestClass2 result = modelMapperConverterService.convert(source, TestClass2.class); assertThat(result, notNullValue()); assertThat(result, is(expected)); then(modelMapper).should().map(source, TestClass2.class); }
@Test(expected = IllegalArgumentException.class) public void convertListShouldThrowIllegalArgumentExceptionWhenSourceListParameterIsNull() { modelMapperConverterService.convert(null, TestClass2.class); }
@Test(expected = IllegalArgumentException.class) public void convertListShouldThrowIllegalArgumentExceptionWhenTargetTypeParameterIsNull() { List<TestClass1> sourceList = Collections.singletonList(new TestClass1(TEST_CLASS_FIRST_ID)); modelMapperConverterService.convert(sourceList, null); }
@Test public void convertListShouldReturnEmptyListWhenEmptyListIsGivenAsSourceList() { List<TestClass1> emptySourceList = Collections.emptyList(); List<TestClass2> result = modelMapperConverterService.convert(emptySourceList, TestClass2.class); assertThat(result, notNullValue()); assertThat(result, empty()); }
@Test public void convertListShouldReturnConvertedListWhenProperParametersWereGiven() { List<TestClass1> sourceList = Arrays.asList( new TestClass1(TEST_CLASS_FIRST_ID), new TestClass1(TEST_CLASS_SECOND_ID) ); List<TestClass2> expected = Arrays.asList( new TestClass2(TEST_CLASS_FIRST_ID), new TestClass2(TEST_CLASS_SECOND_ID) ); for (int i = 0; i < 2; i++) { given(modelMapper.map(sourceList.get(i), TestClass2.class)) .willReturn(expected.get(i)); } List<TestClass2> result = modelMapperConverterService.convert(sourceList, TestClass2.class); assertThat(result, notNullValue()); assertThat(result, not(empty())); assertThat(result, is(expected)); for (int i = 0; i < 2; i++) { then(modelMapper).should().map(sourceList.get(i), TestClass2.class); } }