public interface TestService {
Destination testSourceToDestination(Source source);
List testSourceToDestinationList(List sources);
}
@Service
public class TestServiceImpl implements TestService {
@Autowired
private ModelMapper modelMapper;
@Override
public Destination testSourceToDestination(Source source) {
Destination destination = modelMapper.map(source, Destination.class);
return destination; // a 处
}
@Override
public List testSourceToDestinationList(List sources) {
Type type = new TypeToken>(){}.getType();
List destinations = modelMapper.map(sources, type);
return destinations; // b 处
}
}
测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class TestServiceImplTest {
@Autowired
private TestService testService;
private static Source source1 = new Source("a", "发生的", 24);
private static Source source2 = new Source("b", "发生的");
private static List sources = new ArrayList<>();
static {
List baseClasses1 = new ArrayList<>();
baseClasses1.add(new BaseClass("aa", "发生的111"));
baseClasses1.add(new BaseClass("bb", "发生的222"));
SouSubClass subClass1 = new SouSubClass("aaa", "发生的3333");
subClass1.setGrandSons(baseClasses1);
List baseClasses2 = new ArrayList<>();
baseClasses2.add(new BaseClass("cc", "国防观"));
baseClasses2.add(new BaseClass("dd", "国防观"));
SouSubClass subClass2 = new SouSubClass("ccc", "规范的大哥");
subClass2.setGrandSons(baseClasses2);
List sourceSonList = new ArrayList<>();
sourceSonList.add(subClass1);
sourceSonList.add(subClass2);
Map sourceSonMap = new HashMap<>();
sourceSonMap.put(1, subClass1);
sourceSonMap.put(2, subClass2);
source1.setCreateTime(new Date(System.currentTimeMillis()-98978609));
source1.setSourceSon(subClass1);
source1.setListSon(sourceSonList);
source1.setMapSon(sourceSonMap);
source2.setSourceSon(subClass1);
source2.setListSon(sourceSonList);
source2.setMapSon(sourceSonMap);
sources.add(source1);
sources.add(source2);
}
@Test
public void testSourceToDestination() {
testService.testSourceToDestination(source1);
testService.testSourceToDestination(source2);
}
@Test
public void testSourceToDestinationList() {
testService.testSourceToDestinationList(sources);
}
}