You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improvement: @Generated annotation to mark code that should be ignored by JaCoCo #1528
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.
Reacted by Sean Smith, akayoshi29, bockd, samuel durand, Mikhail Sergeev, Yunzhi-Liu, Adarsh Ramamurthy, Maxim Malygin, Heiko Scherrer, payne911 and 6 more
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
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.
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' .
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.
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.
Reacted by Vitaly E., akayoshi29, Mariano LEANCE, Raimund Klein, Maxim Malygin, Heiko Scherrer, payne911, Tomáš Poledný and Daniel Hammer
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).
@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.
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.
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
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?
Reacted by Ícaro Pires, Joseph cen, dwhitz and Daniel Hammer
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 ).
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.
Activity
sjaakd commentedon Jun 18, 2018
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 wouldJaCoCodetermine coverage on an annotation like@Generated? Isn't this a problem ofJaCoCoempovit commentedon Jun 18, 2018
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.
sjaakd commentedon Jun 23, 2018
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..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' .akayoshi29 commentedon Jul 9, 2019
I would like to have this feature too : ignoring generated code. It can be useful when your project requires a minimum test coverage rate.
bockd commentedon Jul 23, 2019
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.
i-am-Sushil commentedon Dec 12, 2019
Is this implemented ? Status is still open.
I am trying to find out a way to exclude the mapstruct generated files from jacoco coverage.
sjaakd commentedon Dec 12, 2019
yes.. And going to close this one. It would actually make sense to write some basic unit test.
target/generated-sources/I guess is excluded from coverage.OneHalf3544 commentedon Dec 26, 2019
JaCoCo checks compiled classes. It cannot exclude source directories. For the same reason, JaCoCo cannot exclude classes annotated with
@Retention(SOURCE) @interface @Generated.sjaakd commentedon Dec 26, 2019
I'll reopen the issue. I guess my second argument still stands:
MapStruct is not the same as
autoValueorLombok. 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?
akayoshi29 commentedon Dec 27, 2019
@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.
MarianoLEANCE commentedon Jun 6, 2020
As said by Godin the problem is the retention policy of the
@Generatedannotation.In order for JaCoCo to correctly ignore mapstruct's generated code an annotation with runtime retention policy is needed.
mmalygin commentedon Aug 12, 2021
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.
filiphr commentedon Aug 14, 2021
There is currently an open request for this. Have a look at #1574.
@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
@Generatedannotation with the correct retention.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
ChrTall commentedon Dec 12, 2021
@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?
Zegveld commentedon Nov 17, 2022
@filiphr, @sjaakd
Do you think that this one is now covered with the upcoming 1.6.0 where
@AnnotateWith#1574 has been introduced?filiphr commentedon Nov 20, 2022
You are right @Zegveld this one is now covered by the work we did for #1574. If someone wants to use a custom
@Generatedannotation they can do it by using@AnnotateWith( com.example.Generated.class ).Therefore, I am going to close this one
maroodb commentedon Jul 3, 2026
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@AnnotateWitheverywhere.