'java: No implementation was created for ProductMapper due to having a problem in the erroneous element java.util.ArrayList

I'm trying to create very simple JUnit test into Spring Cloud project:

@SpringBootTest(classes = {ProductMapper.class })
public class TestingWebApplicationTests {

    @Test
    public void contextLoads() {
    }

}

import org.mapstruct.Mapper;

@Mapper(config = BaseMapperConfig.class)
public interface ProductMapper {

    ProductDTO toDTO(Product product);

    ProductFullDTO toFullDTO(Product product);

    Product map(ProductFullDTO productDTO);

    ProductFilter toFilter(ProductFilterDTO dto);
}

I get this error when I try to run the test into the latest Intelij.

java: No implementation was created for ProductMapper due to having a problem in the erroneous element java.util.ArrayList. Hint: this often means that some other annotation processor was supposed to process the erroneous element. You can also enable MapStruct verbose mode by setting -Amapstruct.verbose=true as a compilation argument.

Do you know how I can fix this issue?



Solution 1:[1]

Try to reorder your annotation processor factory path, move lombok to the top.

Solution 2:[2]

Use the generated implementation of the mapper within the Spring test configuration:

@SpringBootTest(classes = {ProductMapperImpl.class })

Also remember that the mapper itself should be bound to the Spring context.

Solution 3:[3]

It's not exactly related to MapStruct itself, but maybe it will save some devs a few minutes / hours when searching for the problem.

I had a similar problem in a project, where I was supposed to add new API version and mark old one as deprecated.

When I tried compiling with maven, I got errors

[ERROR] /src/main/java/com/example/MapStructMapperClass.java: No implementation was created for MapStructMapperClass.java due to having a problem in the erroneous element java.util.ArrayList. Hint: this often means that some other annotation processor was supposed to process the erroneous element. You can also enable MapStruct verbose mode by setting -Amapstruct.verbose=true as a compilation argument.

The project uses Lombok, so I checked annotation processor configs. Looked fine and worked properly on a simple, ad hoc created project. Tried bumping MapStruct and Lpmbok versions. Still the same problem.

Then, I noticed a few lines in the log:

[WARNING] COMPILATION WARNING :
[INFO] ------------------------------
[WARNING] /src/main/java/com/example/MyRestController.java: com.example.MyOldService in MyRestController has been deprecated
[INFO] 1 warning
[INFO] ----------------------
[ERROR] COMPILATION ERROR :
[INFO] ----------------------
[ERROR] warnings found and -Werror specified

And the config in maven pom.xml

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <failOnWarning>true</failOnWarning>
  <configuration>
</plugin>

As Javac JDK 8 states:

-Werror
    Terminate compilation if warnings occur.

So, my problem was that I used @Deprecated annotation and someone turned treating compilation warnings as errors on. Due to the project using MapStruct and having pleeeeenty of mappers with the same compilation error, I focused on solving the problem with the annotations processors, completely ignoring those "insignificant" warnings.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 thinwa
Solution 2 Nikolas Charalambidis
Solution 3 AndrewMcCoist