Reputation: 1
I have a project on which I need to report Integration Test (IT) coverage in Sonar. I can generate a coverage report using the jacoco maven plugin so I know the jacoco agent is setup correctly, however Sonar is unable to locate the source files to produce its report.
The project structure is
- Build
- Module1
- Module2
- IT Module
The IT module has no source it just tests classes from Module1 and Module2. I have tried to configure source path using sonar.sources but sonar is still unable to locate the sources - From the logs
[18:01:37.610] Class not found in SquidIndex: com/module1/Class1
I suspect having an empty Integration Test module is a little unusual (more of a system test than IT), is it possible to configure sonar to report on the IT coverage?
Upvotes: 0
Views: 632
Reputation: 11132
I have the same set up with a separate module containing only tests classes to test the integration of other modules. That's perfectly alright.
When analyzing the coverage sonar checks the code of each module against the coverage file that is specified in the sonar.jacoco.itReportPath
property. The default is target/jacoco-it.exec
. So when analyzing Module1
it check for coverage info in Module1/target/jacoco-it.exec
. But as the coverage data is captured in the IT module, you have to point sonar to the file generated when building and testing the IT module.
So what I do for my projects, is to use single coverage data for unit-test coverage for each module and use a central file for the the IT coverage data.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.4.201502262128</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>target/jacoco.exec</destFile>
<append>true</append>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>prepare-it-agent</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${session.executionRootDirectory}/target/jacoco-it.exec</destFile>
<append>true</append>
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
</executions>
</plugin>
The ${session.executionRootDirectory}
property is the root of execution, like the root-module. This also works if you have multi-module with more than one level of nesting.
Now you need to point sonar to use that file when analyzing IT coverage. So you have to set the sonar.jacoco.itReportPath
to that file. Unfortunately, this does not work with the session.executionRootDirectory
property and you have to set the absolute path to the file manually. I do not recommend to specify the absolute path in the pom.xml as this path is specific to your build environment. So either set the path in Sonar or as System property of your build environment. I set it directly in the Sonar Project Settings (Java > Jacoco), for example /opt/buildroot/myProject/target/jacoco-it.exec
.
Now sonar will check that file for the IT coverage analysis of each module.
Upvotes: 1