I tried different approaches that I found here on Stackoverflow. This is the way I know how to use a mapper with MapStruct. I have a Mapper class like this:
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface DummyMapper {
DummyMapper INSTANCE = Mappers.getMapper(DummyMapper.class);
DummyResponseApi modelToApi(DummyResponse DummyResponseModel);
}
And my Unit test:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {DummyMapper.class})
class ServiceClassTest {
private DummyService service;
}
My Service method that I want to test:
public DummyModelApi getSomething() {
DummyModel mapMe = new DummyModel();
return DummyMapper.INSTANCE.modelToApi(mapMe);
}
In older projects I did it the same way like this and had no problems. Now I'm using it in a new project with Spring Boot 2.5.6 and MapStruct 1.5.0.Beta1.
With using @SpringBootTest, as far as I know, Spring is actually starting the application and should create the Mapper class, so I don't understand, why the Mapper is always null?! When I remove the DummyMapper.class in @SpringBootTest, an error appears with "Failed to load application context". That shows me, that the mapper is recognized.
Another thing that I find strange is, that I must use "unmappedTargetPolicy = ReportingPolicy.IGNORE" in my mapper, otherwise I get the error message "Unmapped properties could not found" or something, even though there is definetly the property with the same name in both models. This was always no problem in older projects, don't know why MapStruct is doing weird things now.