Running code coverage with Cobertura and Jacoco
Since you've configure Sonar as
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.jacoco.itReportPath>
${project.basedir}/target/jacoco-t.exec
</sonar.jacoco.itReportPath>
This means you are telling Sonar to reuse the existing report from sonar.jacoco.itReportPath. If there is no existing report, there isn't any coverage.
In my case, I use Cobertura and reuse its report from Maven site generation, using the following configuration properties:
<sonar.java.coveragePlugin>cobertura</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.surefire.reportsPath>
${project.build.directory}/surefire-reports
</sonar.surefire.reportsPath>
<sonar.cobertura.reportPath>
${project.build.directory}/site/cobertura/coverage.xml
</sonar.cobertura.reportPath>
I can get the reuse by using the following command:
mvn clean install site sonar:sonar
I can reproduce your issue by using the following command:
mvn clean install sonar:sonar
The coverage is 0%. Since there is no existing report at the report path.
Then please make sure that there is a report named "jacoco-t.exec" as specified before executing Sonar.
Since I'm not familiar with JaCoCo and do not know which Maven phase that produces the report file, I would suggest to execute the command like the following:
mvn clean test sonar:sonar
or
mvn clean install sonar:sonar
or the same as mine
mvn clean install site sonar:sonar
I hope this may help.
RCross
Updated on June 16, 2022Comments
-
RCross almost 2 years
I have a bit of a problem getting code coverage reports for both Integration Tests and Unit Tests in Sonar for a Maven Plugin project (which uses invoker plugin for the integration tests).
I can't use the default Jacoco coverage tool for the unit tests, as these use Powermock, which results in 0% coverage for classes using that. On the other hand, I can't find a reliable way to get results for the Groovy-based integration tests without using Jacoco.
So what I need is for Cobertura to produce a Unit Test report, Jacoco to produce an Integration Test report, and for Sonar to be able to read the lot.
I tried using the example here https://github.com/Godin/sonar-experiments/tree/master/jacoco-examples/maven-invoker-plugin-example but eliminating the executions bound to the test phase, but I then get a unit test coverage of '-' in Sonar. I think the reason for this is that to get this method to work, I need to speicify Jacoco as the core coverage tool for Sonar.
Any ideas on a way round this? My pom.xml follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.acme.myproj.plugins</groupId> <artifactId>slice2java-maven-plugin</artifactId> <version>0.1-SNAPSHOT</version> <packaging>maven-plugin</packaging> <name>Slice2Java Maven Plugin</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <sonar.exclusions>**/generated*/*.java</sonar.exclusions> <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin> <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis> <sonar.jacoco.itReportPath>${project.basedir}/target/jacoco-it.exec</sonar.jacoco.itReportPath> </properties> <dependencies> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.apache.maven.plugin-tools</groupId> <artifactId>maven-plugin-annotations</artifactId> <version>3.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-utils</artifactId> <version>3.0.8</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.9.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-core</artifactId> <version>1.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>1.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.5</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-plugin-plugin</artifactId> <version>3.2</version> <configuration> <goalPrefix>slice2java</goalPrefix> <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound> </configuration> <executions> <execution> <id>mojo-descriptor</id> <goals> <goal>descriptor</goal> </goals> </execution> <execution> <id>help-goal</id> <goals> <goal>helpmojo</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <profiles> <profile> <id>run-its</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-invoker-plugin</artifactId> <version>1.8</version> <configuration> <debug>true</debug> <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo> <pomIncludes> <pomInclude>*/pom.xml</pomInclude> </pomIncludes> <postBuildHookScript>verify</postBuildHookScript> <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath> <settingsFile>src/it/settings.xml</settingsFile> <goals> <goal>clean</goal> <goal>test-compile</goal> </goals> </configuration> <executions> <execution> <id>integration-test</id> <goals> <goal>install</goal> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.5.3.201107060350</version> <configuration> <includes>com.acme.*</includes> </configuration> <executions> <execution> <id>pre-integration-test</id> <phase>pre-integration-test</phase> <goals> <goal>prepare-agent</goal> </goals> <configuration> <destFile>${project.build.directory}/jacoco-it.exec</destFile> <propertyName>invoker.mavenOpts</propertyName> </configuration> </execution> <execution> <id>post-integration-test</id> <phase>post-integration-test</phase> <goals> <goal>report</goal> </goals> <configuration> <dataFile>${project.build.directory}/jacoco-it.exec</dataFile> <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>