Sonarqube - Remove this unused private field Code Smell using Lombok @data

Viewed 6031

I've got a bunch of Code Smells in my Java project around bits of code like this:

@Data
public class Foobar extends Foo { 
    private String baz;
}

My lombok.config sits alongside the pom.xml and looks like:

config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true

This brought up the code coverage numbers, not has not cleared the Code Smells.

Seems I'm not the only person encountering this problem.

In terms of versions:

  • Lombok 1.18.8 (also tried with 1.18.10)
  • Jacoco 0.8.4
  • SonarQube 7.9.1.27448
  • SonarQube Scanner 4.0.0.1744

I've seen a few similar questions on this, but they are all ~5 years old, so I dont believe the answers are still valid.

1 Answers

I have faced same issue with you a few months ago. My jacoco version is 0.8.2 and lombok is v1.16.16. I remember the problem was gone after I add lombok.config under root directory of the project. Just notice if you define your own constructors for a class that already marked as Data, then the coverage will be calculated by how many constructors your tests were called.

And if this didn't help. You can find a workaround by excluding them in either Jacoco config / SonarQube config

Example for excluding in jacoco:

<plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.2</version>
            <configuration>
                <excludes>
                    <exclude>**/your_class_path/**</exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <!-- attached to Maven test phase -->
                <execution>
                    <id>report</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

And for SonarQube, you can use -Dsonar.inclusions parameter for inclusion and -Dsonar.exclusions parameter when calling mvn sonar:sonar

Related