Lombok annotations vs code coverage in Cobertura or similar tool
Asked Answered
S

3

13

Configure cobertura to ignore certain blocks of code

From what I have read from above question, there's no way in Cobertura to exclude given code part from being tested versus having coverage in tests.

Is that true? / Is it possible in any simmilar tool?

I'm usuing Lombok annotations @Getter, @Setter and so on, which are great, but they result in being 'red' in coverage report, even if I'm testing getter and setter methods. - I would like to do something with that... Is there any way to fix this?

Stefanstefanac answered 23/4, 2012 at 7:43 Comment(0)
L
4

Isn't it possible to first run delombok over the code under test, compile it and then instrument it by Cobertura?

Disclosure: I am one of the Project Lombok developers

Lookeron answered 23/4, 2012 at 17:37 Comment(3)
How would this help? Delombok doesn't get rid of the getters/setters, which are still shown "red" in coverage.Skuld
Also in my case, some test code require generated hashcode and equals methods to work correctly. With delomboked code, the tests would break.Nicoline
Why would the tests break on equals and hashCode? Is there something wrong with the generated code?Lookeron
E
4

Lombok adds a @javax.annotation.Generated annotation (1). But this annotation has source retention, i.e. your coverage tool can't see it any more :-(

Lombok 1.16.14 just fixed issue-1014 by adding an option to generate a @lombok.Generated annotation instead; just configure lombok.addLombokGeneratedAnnotation(2).

Cobertura can be configured to skip methods with some annotation, so most of the generated code won't count against your coverage.

JaCoCo doesn't provide a configuration mechanism to skip on certain annotations. Issue-15 tries to fix this (and much more) without requiring any configuration.

(1) If not disabled with lombok.addGeneratedAnnotation which is now deprecated and should be replaced with lombok.addJavaxGeneratedAnnotation
(2) see lombok config
(3) see this comment

Euroclydon answered 29/3, 2017 at 10:52 Comment(0)
W
0

Lombok creates additional code that has branches for certain methods. If, for example, you add @Data and your object does not override equals, you'll get a bunch of branches in the equals method for whether each of your member variables is null or not. So if you want to use Lombok, you'll just have to accept the lower branch counts or figure out some consistent way to have cobertura not count autogenerated code.

You can use Delombok to get a better idea of why the coverage is lower, but you won't be able to raise it without testing those branches that were autogenerated.

An earlier version of Lombok (0.12) interacted with cobertura in such a way that lombok generated code was not counted in coverage.

Another thing you could add is the 'ignoreTrivial' flag in Cobertura. This should take care of auto-generated getters and setters, but not things like equals() or hashCode().

To get coverage on equals() and hashCode() you can use EqualsVerifier.

Windom answered 3/5, 2020 at 17:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.