Skip to content

Instantly share code, notes, and snippets.

@dariahervieux
Last active May 31, 2024 03:59
Show Gist options
  • Save dariahervieux/49a644fb4a12c94558f87219169ed9f7 to your computer and use it in GitHub Desktop.
Save dariahervieux/49a644fb4a12c94558f87219169ed9f7 to your computer and use it in GitHub Desktop.
Java : Jacoco + Mapstruct/Lombok

Code coverage and generated code

Jacoco

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

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

Lombok can be configured to add @lombok.Generated annotations to all generated nodes where possible.

lombok.addLombokGeneratedAnnotation = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment