Integration Test Coverage in SonarQube from JaCoCo Maven plug-in showing 0%

前端 未结 5 665
天命终不由人
天命终不由人 2021-02-05 14:58

We have a multi-module multi-language maven java project with coverage analysis with jacoco. The main part of the modules is backend (Java code) with a REST API and our webapp m

相关标签:
5条回答
  • 2021-02-05 15:10

    I had the same experience when I was configuring JaCoCo for Jersey. I ended up applying the bisection method in order to find out which modules are causing the code coverage for the whole project to drop to 0%.

    If I remember it correctly, the biggest surprise was maven-shade-plugin which somehow corrupted the association between the collected coverage and the location of classes which caused Sonar to show 0% code coverage. I ended up disabling its execution by binding it to phase none in order to fix it (see this commit).

    As an example, this is how to extend a pom.xml of a sub-module in order to have working sonar (assuming, sonar is executed with sonar maven profile).

    <profile>
        <id>sonar</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <!-- disable parallel execution so that JaCoCo listener can properly work -->
                        <parallel>none</parallel>
                        <perCoreThreadCount>false</perCoreThreadCount>
                        <forkCount>1</forkCount>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-shade-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>shade-archive</id>
                            <phase>none</phase>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>org.ow2.asm</groupId>
                <artifactId>asm-debug-all</artifactId>
                <optional>false</optional>
            </dependency>
        </dependencies>
    </profile>
    
    0 讨论(0)
  • 2021-02-05 15:11

    I use JUnit and in my case the issue was because of having TestNG dependency in my pom.xml in addition to JUnit. After removing this unnecessary dependency, everything started to work as expected.

    0 讨论(0)
  • 2021-02-05 15:18

    Considering other answers I want do add one thing, which is important to show test coverage on Sonar:

    1. you have to build project before expecting coverage to be shown: mvn clean install
    2. you have to build without -DskipTests
    3. you have to keep right configurations for plugin which run tests (surefire, failsafe, ... plugin) - coverage will be shown considering only those tests which are included in plugin configurations by <includes> or similar configurations.
    0 讨论(0)
  • 2021-02-05 15:21

    After fighting very long time with Jacoco and Maven only this way it is working for me:

    1. Create profile in pom.xml

      <profile>
        <id>coverage-per-test</id>
        <build>
          <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.19.1</version>
              <configuration>
               <!-- disable parallel execution so that JaCoCo listener can properly work -->
                <parallel>none</parallel>
                <perCoreThreadCount>false</perCoreThreadCount>
                <forkCount>1</forkCount>
                <properties>
                  <property>
                    <name>listener</name>
                    <value>org.sonar.java.jacoco.JUnitListener</value>
                  </property>
                </properties>
              </configuration>
            </plugin>
          </plugins>
        </build>
      
        <dependencies>
          <dependency>
            <groupId>org.sonarsource.java</groupId>
            <artifactId>sonar-jacoco-listeners</artifactId>
            <version>3.10</version>
            <scope>test</scope>
          </dependency>
        </dependencies>
      </profile>
      
    2. Update maven surefire plugin to latest version

    3. Execute commands:

      mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Pcoverage-per-test -Dmaven.test.failure.ignore=true
      
      mvn sonar:sonar
      

    Similar as describe in github sonar example

    0 讨论(0)
  • 2021-02-05 15:29

    Im using a similar setup and everything works perfectly with the following configuration:

    However the maven-surefire-plugin was causing troubles initially. Thats why I had to add the @{argLine} in the argLine tag, since the jacoco-maven-plugin:prepare-agent goal is executed before the maven-surefire-plugin. (see https://stackoverflow.com/a/25774988)

    Hope I could help

    <plugins>
        <plugin>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>3.4.0.905</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
    
            <executions>
                <execution>
                    <phase>test</phase>
                </execution>
            </executions>
            <configuration>
                <!-- the @{argLine} is needed for proper jacoco execution -->
                <argLine>@{argLine} -Xmx256m</argLine>
            </configuration>
        </plugin>
    
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</version>
            <executions>
                <execution>
                    <id>jacoco-initialize</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <!-- generate fancy html report -->
                <execution>
                    <id>jacoco-site</id>
                    <phase>package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    
    0 讨论(0)
提交回复
热议问题