I have a simple Java program which uses the Lombok Annotation - #Builder.
I am testing my code using junit and everytime I run my unit tests, my coverage is always below 50% inspite of the fact that I am testing my entire code.
I looked into the junit generated code coverage and I saw that it was the Lombok annotation which was making the coverage drop.
I see something like:
toString() - 0%
build() - 0%
MyMethod.MyMethodBuilder() - 0%
How do I test these methods for the #Builder annotation? Or the only way to improve coverage is to exclude those from the test coverage?
Starting JaCoCo v0.8.0, you should create lombok.config file to the root of your project and add following content to it:
lombok.addLombokGeneratedAnnotation = true
This will add #Generated annotation to Lombok generated files and JaCoCo will know that code coverage of such a code should be ignored.
Credits to original author here
Create lombok.config file and add texts below:
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
config.stopBubbling = true is telling Lombok that this is the root directory and that it shouldn’t search parent directories for more configuration files (you can have more than one lombok config files in different directories/packages).
lombok.addLombokGeneratedAnnotation = true is telling Lombok to add the #lombok.Generated annotation to all generated methods.
Decompile class which contains lombok annotation to see how lombok has generated source code and then write junits considering that code.
e.g. In case of #ToString, make explicit call to toString method on class object i.e. obj.toString();
Better to use pl.pojo library to test the pojo model classes.
Related
I wrote a custom annotation to ignore classes and methods while generating Jacoco coverage report. Please refer to code below:
#Target({ElementType.TYPE,ElementType.METHOD,ElementType.CONSTRUCTOR})
public #interface ExcludeFromJacocoGeneratedReport {
}
This is working fine in my local but when I am running a Jenkins pipeline, classes and methods marked with this custom annotation are not getting ignored in Jacoco coverage report.
Please guide me if I have missed something or some explicit configuration is required before running jenkins pipeline in this case.
I tried to get a good coverage file in my java spring project, I mean; see where my tests are good and where they are not. But my problem is that #Data generate a lot of function/class that I don't want to test, and just ruin my coverage.
I already search for a solution, but there is nothing to fix my problem. Looks like Jacoco have something to fix my problem but not Pitest. Maybe Im just not taking the problem right.
Since version 1.2.1 Pitest ignores any method or class annotated with Generated.
You can configure Lombok to add the annotation by adding the following in lombok.config :
lombok.addLombokGeneratedAnnotation = true
After running test and generating coverage report via Jacoco, I realized that there is a static method where a #Data annotation is used and the line of it is marked yellow indicating that it is not tested.
Report
So, is it normal or how can I test that line?
jacoco can ignore code generated by lombok.
put lombok.config in your project’s root
lombok.addLombokGeneratedAnnotation = true
jacoco Release 0.8.0 (2018/01/02)
Methods annotated with #lombok.Generated to better integrate with Lombok >= 1.16.14. Initial analysis and contribution by Rüdiger zu Dohna (GitHub #513).
Methods annotated with #groovy.transform.Generated to better integrate with Groovy >= 2.5.0. Thanks to Andres Almiray for adding the annotation to Groovy (GitHub #610).
jacoco Release 0.8.2 (2018/08/21)
Classes and methods annotated with annotation whose retention policy is runtime or class and whose simple name is Generated are filtered out during generation of report (GitHub #731).
Seems opinion based, but in my opinion there is no point for testing generated code from an external dependency. I would do noting about it and move on.
Sample multi-module project: https://github.com/overfullstack/my-lab/tree/master/lombok-lab
Sample single module project: https://github.com/overfullstack/untitled
I have the following lombok.config:
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
lombok.extern.findbugs.addSuppressFBWarnings = true
But when I see the generated code in build/generated/sources/delombok/java/main/... I don't see #Generated annotations. This is affecting my Jacoco test coverage. Please help. Thanks.
(I observed from my single module project that #Genearated annotations are being added to classes in build/classes/java/main/... but not to build/generated/sources/delombok/java/main/..., is this a bug or expected?)
I am getting a lot code smells from lombok generated code in Sonar. F.E.:
Method Dto.hashCode() stores return result in local before immediately returning it
Dto.equals(Object)
is excessively complex, with a cyclomatic complexity of 58
How can I point out sonar that this should be skipped from analyze?
UPDATE
I've tried it already. My lombok.config file in root directory is:
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
lombok.equalsAndHashCode.callSuper = call
It doesn't helps
I've tried it already: sonarqube + lombok = false positives
I've updated: sonar-project.properties in root directory to:
sonar.sources=src/main
sonar.tests=src/test
sonar.language=java
sonar.java.binaries=build/classes
sonar.junit.reportPaths=build/test-results/test/
sonar.jacoco.reportPaths=build/jacoco/jacocoTest.exec
sonar.java.libraries=.gradle/caches/**/lombok-*.jar
It doesn't work either.
Please don't close it. It is not duplication.
I just had the same issue. I am using sonar-scanner and figured out that it needs to set Lombok jar file using command line argument.
For example:
sonar-scanner -D sonar.java.libraries=/home/gitlab-runner/.gradle/caches/modules-2/files-2.1/org.projectlombok/lombok/1.18.10/625fc0055674dff70dbc76efa36d0f2c89b04a24/lombok-1.18.10.jar
Now SonarQube does not show any issues related with Lombok annotations.
Methods generated by lombok need to be annotated with #Generated. Sonarqube will then ignore them.
Just add a file lombok.config in the project root directory, with the following content:
lombok.addLombokGeneratedAnnotation=true
Be sure that lombok.jar is well inside the directory referenced in the sonar.java.libraries property.
I had the same problem, I added the property but I had put a reference to the directory of my runtime package that did not contains the lombok.jar!
lombok.jar is used at compile time and useless at runtime so we avoid to add it inside this directory.