I am using MapStruct in my project. I have an entity object in which i would like to map to my domain object. The entity object comes from running a SQL query.
Inside the entity object, it has a field in which it contains a List of another type (due to a 1 to many mapping). This is my mapping:
@Mapping(target = "listOfDomainB", source = "entityA.list", qualifiedByName = "mapList")
DomainA map(EntityA entityA);
@Named("mapList")
default List<DomainB> mapList(List<EntityB> entityB) {
return entityB.stream()
.map(EntityB -> new EntityB()
.name(EntityB.getName()))
.collect(Collectors.toList());
}
It does the job but this mapper is being used in my service class (via constructor injection... private final) and when I am writing my unit test for that service class, i am not mocking the mapper as I want to use the implementation of the mapper. But the unit test fails as it cannot access the default method mapList because it's not in the implementation class. The implementation class calls this default method but when doing (.toCallRealMethod() on the mapper mock i.e. when(mapperImpl.map()).thenCallRealMethod(), the impl class doesn't call the mapList)
Any idas?