Uploaded image for project: 'Subscription Watch'
  1. Subscription Watch
  2. SWATCH-3424

Fix code coverage to exclude generated classes

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Obsolete
    • Icon: Normal Normal
    • None
    • None
    • None
    • None

      When running `./mvnw verify -Dcoverage`, the jacoco report is generated at `coverage-report/target/site/jacoco-aggregate`:

      The covered percentage is quite slow comparing it with what we got from Sonar report:

      The problem is that the report from Jacoco in Maven is not excluding the generated classes.

      Acceptance Criteria

      • The Jacoco plugin is not excluding the generated sources from the openapi or the jsonschema plugins. In order to configure this, we need to generate the generated sources into a separated package including the name generated. For example, from:
      <plugin>
              <groupId>org.jsonschema2pojo</groupId>
              <artifactId>jsonschema2pojo-maven-plugin</artifactId>
              <configuration>
                <targetPackage>com.redhat.swatch.contract.model</targetPackage>
                <outputDirectory>${project.build.directory}/generated-sources/src/main/java</outputDirectory>
                <includeAdditionalProperties>false</includeAdditionalProperties>
      

      To:

      <plugin>
              <groupId>org.jsonschema2pojo</groupId>
              <artifactId>jsonschema2pojo-maven-plugin</artifactId>
              <configuration>
                <targetPackage>com.redhat.swatch.contract.model.generated</targetPackage>
                <outputDirectory>${project.build.directory}/generated-sources/src/main/java</outputDirectory>
                <includeAdditionalProperties>false</includeAdditionalProperties>
      

      And next, configure the plugin to exclude these packages:

      <build>
          <plugins>
            <plugin>
              <groupId>org.jacoco</groupId>
              <artifactId>jacoco-maven-plugin</artifactId>
              <executions>
                <execution>
                  <id>report</id>
                  <phase>verify</phase>
                  <goals>
                    <goal>report-aggregate</goal>
                  </goals>
                  <configuration>
                    <dataFileIncludes>
                      <dataFileInclude>**/jacoco.exec</dataFileInclude>
                    </dataFileIncludes>
                    <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
                    <excludes>
                      <exclude>**/generated/**</exclude>
                      <exclude>**/openapi/**</exclude>
                    </excludes>
                  </configuration>
                </execution>
              </executions>
            </plugin>
          </plugins>
        </build>
      

      Note that the openapi Maven plugin already generates the API under the openapi package, so we can use it to exlude it from the jacoco report. But, we need to double check that all the modules are configured to append the openapi name in the package.

      • Configure the Jacoco plugin to exclude the generated classes, so the report is similar to what we have in Sonar Main.

        1. Screenshot From 2025-03-28 07-03-06.png
          132 kB
          Jose Carvajal Hilario
        2. Screenshot From 2025-03-28 07-04-35.png
          216 kB
          Jose Carvajal Hilario

              jcarvaja@redhat.com Jose Carvajal Hilario
              jcarvaja@redhat.com Jose Carvajal Hilario
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: