When analysing code coverage jacoco analyses compiles classes. If you have generated code, jacoco will take it for analysis, unless your classes are annotated with an annotation havin 'Generated' in its name.
The filter used by jacoco to filter out all classes annotated with ..Generated
is AnnotationGeneratedFilter
Mapstruct generates files with @Generated annotation, which has SOURCE retention policy.
Starting from 1.6.0, mapstruct offers @AnnotateWith annotation. For jacoco to ignore the classes generated by mapstruct, you can create you custom annotation:
/**
* Annotation to put on Mapstruct mappers for generated classes to keep the annotation.
* See https://github.com/mapstruct/mapstruct/issues/1528
* https://github.com/mapstruct/mapstruct/issues/1574 (Mapstruct milestone 1.6.0)
*/
@Retention(CLASS)
public @interface GeneratedMapper {
}
And annotate each of your mappers, for which you don't wantthe generated code to be analysed for test code coverage:
@AnnotateWith(GeneratedMapper.class)
@Mapper(componentModel = "spring")
public interface ExampleMapper {
...
}
Lombok can be configured to add @lombok.Generated annotations to all generated nodes where possible.
lombok.addLombokGeneratedAnnotation = true