0

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.

C. Oltan
  • 111
  • 1
  • 3
  • 7
  • Can you paste the error? Because the way you're instantiating the mapper shouldn't impact on running a SpringBootTest or running a simple unit test as the mapper is instantiated as a singleton at the load of the class. – Sergio Lema Nov 19 '21 at 10:14
  • @Sergio Lema: There is no error. When I make a breakpoint where the mapper should return the mapped object, all properties of the object are null, I get a NullPointerException and my tests are failing of course. – C. Oltan Nov 19 '21 at 10:24
  • You mean that DummyMapper.INSTANCE is null or the fields of the object passed to modelToApi are null? Paste your NullPointerException stacktrace, it will help a lot. – Sergio Lema Nov 19 '21 at 10:31
  • @Sergio Lema: The DummyMapper.INSTANCE is not null. The fields of the object returned by the mapping method are null. I think I have so solve the problem first why the error message "mapper\DummyMapper.java:40: error: Unmapped target property: "simpleText". DummyResponseApi modelToApi(DummyResponse dummyResponseModel);" appears when I run the task assemble. I removed "unmappedTargetPolicy = ReportingPolicy.IGNORE" in the mapper. It should be no problem to simply map 1 property with the same name, right? – C. Oltan Nov 19 '21 at 11:10
  • I added the method annotation "@Mapping(target = "simpleText", source = "simpleText") DummyResponseApi modelToApi(DummyResponse dummyResponseModel);" I get the error "No property named "simpleText" exists in source parameter(s). Type "DummyResponse" has no properties. @Mapping(target = "simpleText", source = "simpleText")", but the property simpleText exists in DummyResponse ``` @Data @Accessors(chain = true) @AllArgsConstructor @NoArgsConstructor public class DummyResponse { private String simpleText; } ``` – C. Oltan Nov 19 '21 at 11:14

1 Answers1

0

Omg, I found the solution. Now I know why I had to add "@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)" in my mapper.

Found the solution here: https://stackoverflow.com/a/69649688/8743351

Lombok needs to be before mapstruct in pom.xml/gradle.build.

C. Oltan
  • 111
  • 1
  • 3
  • 7