SonarQube
Lombok
Code Coverage
Java
Software Testing

Excluding Lombok classes from Sonar coverage report

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

SonarQube is a powerful tool for continuous inspection of code quality. It automatically detects bugs, code smells, and vulnerabilities in your code. However, when using libraries like Lombok in Java projects, it can lead to a misrepresentation of code coverage. Lombok is a popular library that reduces boilerplate code, but its generated methods (e.g., getters, setters, equals, hashCode) can clutter SonarQube's coverage reports. This article explores how to exclude Lombok-generated methods from SonarQube coverage reports to provide a clearer, more accurate view of your codebase's quality.

Understanding Code Coverage with Lombok

When Lombok is used, the IDE does not directly show the generated code, but the compiled Java files include these methods. Consequently, when SonarQube runs code analysis, these Lombok-generated methods can appear as untested lines, skewing the actual test coverage metrics. To address this, the primary approach involves tweaking the SonarQube setup to exclude them from the report.

Excluding Lombok-Generated Classes

To exclude Lombok-generated code from SonarQube's coverage report, you can utilize SonarQube's configuration capabilities. Below are some methods to achieve this:

  1. Update pom.xml for Maven Projects:
    Using the SonarQube Maven plugin, you can configure exclusions directly in the pom.xml . Consider excluding Lombok methods using specific annotations or patterns:
  • Adopt Naming Conventions: Ensure that your Lombok-generated files follow a recognizable naming convention for easy exclusion.
  • Separate Directories for Generated Code: Organize your file structure to keep Lombok-generated classes in designated directories for more straightforward exclusion.
  • Customize Analysis Profile: SonarQube allows customization of the ruleset. You might consider setting rules that ignore specific types or patterns recognizable as generated by Lombok.
    • Ensure your build tool (e.g., Gradle, Maven) is configured to understand these exclusions, as they may impact other phases of your build pipeline.
    • Explore custom plugins or additional community plugins that provide enhanced handling for Lombok.
    • Maintain your SonarQube configuration to reflect any changes in your Lombok utilization or project structure.

Course illustration
Course illustration

All Rights Reserved.