Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get correct coverage in sonar for unit and integration tests in separate maven modules

My project setup is simple (all source available at github):

parent
↳ backend
↳ client
↳ integration-tests

And after runing maven:

mci sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=12...9

I see that unit and integration tests are visible for sonar, but coverage from IT is not. sonar coverage sonar coverage detailed

For Intelij IDEA jacoco-it.exec looks fine: idea coverage

I'm assuming that culprit is here:

[INFO] Sensor JaCoCoSensor [java]
[INFO] No JaCoCo analysis of project coverage can be done since there is no class files.
[INFO] Sensor JaCoCoSensor [java] (done) | time=1ms

So I did small hack (in short: copied all source files to integration-test module):

    <properties>
        <sonar.sources>${basedir}/target/copied</sonar.sources>
    </properties>
    [...]
        <!-- hack to generate coverage reports -->
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>${maven-resources-plugin.version}</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${sonar.sources}</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${basedir}/../backend/src/main/java</directory>
                                <filtering>false</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>${sonar.sources}</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

But now all my classes ale duplicated (sonar shows also classes from ../target/copied dir): sonar coverage duplicated

Sonar version: 6.5
Jacoco maven plugin: 0.7.5.201505241946 (also tried lastest 0.7.9)

Any ideas what should I do here?

like image 708
Pawel Avatar asked Aug 18 '17 18:08

Pawel


Video Answer


1 Answers

Looks like I have answer for that question:

  1. Report wasn't generated because post-unit-test execution was in the wrong phase. Instead of <phase>test</phase> I now have <phase>verify</phase>

  2. I had a wrong goal for post-integration-test. The change was from <goal>report-integration</goal> to <goal>report-aggregate</goal>:

    • This goal allows me to create coverage reports when tests are in separate projects from the code under test.
  3. Added properties: <jacoco.itReportPath>${project.basedir}/../integrations-tests/target/jacoco-it.exec</jacoco.itReportPath> and <sonar.jacoco.reportPaths>${jacoco.itReportPath},${project.build.directory}/jacoco-it.exec,${project.build.directory}/jacoco.exec</sonar.jacoco.reportPaths>

All those chages and update project available on github

like image 147
Pawel Avatar answered Oct 14 '22 22:10

Pawel