Skip to content

Improvement: @Generated annotation to mark code that should be ignored by JaCoCo #1528

Closed

Description

@empovit

It would be nice to have an annotation with CLASS retention policy that can be ignored by code coverage tools like JaCoCo. This should work similar to how it's done for Immutables and Lombok.

Activity

  1. sjaakd commented on Jun 18, 2018

    @sjaakd
    Contributor

    Is there any annotation that you have in mind? My setup is such that generated code is in target\generated-source.. (not subjected to code-coverage analysis) How would JaCoCo determine coverage on an annotation like @Generated? Isn't this a problem of JaCoCo

  2. empovit commented on Jun 18, 2018

    @empovit
    Author

    You need a brand new annotation for that. Ignoring all of the generated code is not good enough because there may be custom mappings that you do want to test.
    It is a problem of both JaCoCo and Mapstruct. If you follow the links I've provided, you can see that it is possible to open an issue for JaCoCo team and ask them to ignore code annotated by Mapstruct. However, you need a suitable annotation in the first place.

  3. sjaakd commented on Jun 23, 2018

    @sjaakd
    Contributor

    Still it feels strange: each code generator should fix a problem that seems to originate somewhere else. For code originating from Lombok I can see a point: it replaces your own class definition (and is doing something that is not intended by JSR-269). In my own code this is solved by some reflective testing of getters and setters in a package (a TestAllDto) marker test case will do the trick..

    Ignoring all of the generated code is not good enough because there may be custom mappings that you do want to test.

    Custom mappings are in your src\main\java\ right? A technique would be to inherit from that class yourself (if the mapper is abstract) and cover it with unit test. As for coverage on generated sources: you have a choice. Do - or don't do unit testing on those. Personally I prefer ' do' .

  4. akayoshi29 commented on Jul 9, 2019

    @akayoshi29

    I would like to have this feature too : ignoring generated code. It can be useful when your project requires a minimum test coverage rate.

    How would JaCoCo determine coverage on an annotation like @generated?
    Jacoco is based on compiled classes. So, we need a mapstruct generated annotation in mapstruct compiled class and then, Jacoco can ignore class with this annotation.

  5. bockd commented on Jul 23, 2019

    @bockd

    I agree with @akayoshi29 that it would be nice to be able to ignore the generated code so that it will not affect the code coverage of our project.

  6. i-am-Sushil commented on Dec 12, 2019

    @i-am-Sushil

    Is this implemented ? Status is still open.
    I am trying to find out a way to exclude the mapstruct generated files from jacoco coverage.

  7. sjaakd commented on Dec 12, 2019

    @sjaakd
    Contributor

    Is this implemented ? Status is still open.

    yes.. And going to close this one. It would actually make sense to write some basic unit test.

    I am trying to find out a way to exclude the mapstruct generated files from jacoco coverage.

    target/generated-sources/ I guess is excluded from coverage.

  8. OneHalf3544 commented on Dec 26, 2019

    @OneHalf3544

    target/generated-sources/ I guess is excluded from coverage.

    JaCoCo checks compiled classes. It cannot exclude source directories. For the same reason, JaCoCo cannot exclude classes annotated with @Retention(SOURCE) @interface @Generated.

  9. sjaakd commented on Dec 26, 2019

    @sjaakd
    Contributor

    I'll reopen the issue. I guess my second argument still stands:

    It would actually make sense to write some basic unit test.

    MapStruct is not the same as autoValue or Lombok. There are many options that interplay, especially when combined with hand-written-methods and it is possible to create unwanted behaviour. So unit testing still makes sense.

    What we could do is add an processor option to use our own Generated (retention class) annotation, which I believe will be recognised by Jacoco (reading the documentation).

    @filiphr : WDYT?

  10. reopened this on Dec 26, 2019
  11. akayoshi29 commented on Dec 27, 2019

    @akayoshi29

    @sjaakd I agree with you. Some basic unit tests are still necessary to check the hand-written mapping is good but not all the generated class (all the null checking for example).
    The Generated annotation will be nice. I believe Jacoco can recognize it automatically too. If not, we could ask them to do it.

  12. MarianoLEANCE commented on Jun 6, 2020

    @MarianoLEANCE

    As said by Godin the problem is the retention policy of the @Generated annotation.

    In order for JaCoCo to correctly ignore mapstruct's generated code an annotation with runtime retention policy is needed.

  13. mmalygin commented on Aug 12, 2021

    @mmalygin

    The javax.annotation.Generated annotation used by mapstruct has retention policy SOURCE which means the annotation will be removed from .class files and Jacoco will not be able to detect it.
    As solution, another annotation with retention policy CLASS or RUNTIME is required. the CLASS is preferred because it will be stored in .class files but will not be loaded to memory in runtime. See lombok.Generated for example. As I know Jacoco checks for annotations which contain "Generated" substring in its name.
    Another option is to add an option to mapstruct config that will allow us to add a custom annotations to generated classes. In this case I can configure mapstruct to add lombok.Generated to all generated classes and Jacoco will skip them in coverage report.

  14. filiphr commented on Aug 14, 2021

    @filiphr
    Member

    Another option is to add an option to mapstruct config that will allow us to add a custom annotations to generated classes. In this case I can configure mapstruct to add lombok.Generated to all generated classes and Jacoco will skip them in coverage report.

    There is currently an open request for this. Have a look at #1574.

    What we could do is add an processor option to use our own Generated (retention class) annotation, which I believe will be recognised by Jacoco (reading the documentation).

    @sjaakd I missed your question. I believe that doing #1574 is the way forward for this. If people can add whatever they want they can add their own @Generated annotation with the correct retention.

    Some basic unit tests are still necessary to check the hand-written mapping is good but not all the generated class (all the null checking for example).

    I do not agree that only hand-written mapping should be tested. You can influence the way the mapping is done by different properties on @Mapper, @BeanMapping, @Mapping, using nested source mapping, nested target mapping, etc. All of that should be properly tested if you ask me.

    One other approach for ignoring code is if you instruct MapStruct to put the code in a special package and you tell JaCoCo to ignore that.

    @sjaakd I am keen on closing this one

  15. ChrTall commented on Dec 12, 2021

    @ChrTall

    @sjaakd
    When you have Dtos that have javax Anntotations such as @NotNull for fields you do not have to write unit tests that cover the Null Checks in the Validator, because you won´t have null fields.
    I agree that you should test the mappers, but in such situations it would be better if you could exclude it from jacoco. Are there any updates on when this feature is implemented?

  16. Zegveld commented on Nov 17, 2022

    @Zegveld
    Contributor

    @filiphr, @sjaakd
    Do you think that this one is now covered with the upcoming 1.6.0 where @AnnotateWith #1574 has been introduced?

  17. filiphr commented on Nov 20, 2022

    @filiphr
    Member

    You are right @Zegveld this one is now covered by the work we did for #1574. If someone wants to use a custom @Generated annotation they can do it by using @AnnotateWith( com.example.Generated.class ).

    Therefore, I am going to close this one

  18. maroodb commented on Jul 3, 2026

    @maroodb

    Hi,

    Sorry to revive this thread.

    Using @AnnotateWith(com.example.Generated.class) works, but it introduces unnecessary verbosity. It would be even better if there were a global configuration option that allowed users to specify a custom annotation once, rather than having to add @AnnotateWith everywhere.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions