2

I have a project, for which I have already have generated jacoco.exec report files using jacoco plugin for junit test case and integration test.

This is the maven properties I use for sonar:

<sonar.jacoco.itReportPath>${project.basedir}/target/it/jacoco.exec</sonar.jacoco.itReportPath>
<sonar.jacoco.reportPath>${project.basedir}/target/junit/jacoco.exec</sonar.jacoco.reportPath>
<!-- Tells Sonar to run the unit tests -->
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<!-- Tells Sonar to use JaCoCo as the code coverage tool -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.language>java</sonar.language>

I created profile for sonar like below:

<profile>
  <id>sonar-run</id>
  <activation>
    <activeByDefault>true</activeByDefault>
  </activation>
  <properties>
    <sonar.jdbc.url>
      jdbc:mysql://localhost:3306/sonar
    </sonar.jdbc.url>
    <sonar.jdbc.username>sonar</sonar.jdbc.username>
    <sonar.jdbc.password>123qwe</sonar.jdbc.password>
    <sonar.host.url>
      http://localhost:9000/
    </sonar.host.url>
  </properties>
</profile>

The problem I face is, I am able to see the unit test code-coverage in sonar, but I am not able to see the integration-test code-coverage.

I run the maven command many ways some thing like this:

mvn verify -P sonar-run sonar:sonar
mvn verify -P sonar-run sonar:sonar -Dtests=false
mvn clean install -P sonar-run sonar:sonar

but still I am not able to see the integration-test code coverage in sonar, am I missing some step?

I use sonar 4.0, java-7.

Note: For us the jacoco.exec files are already generated in some other run, I just want to seed them to sonar if possible, this way we will gain some performance improvement.

2
  • Okay, thanks for clarifying and editing the question accordingly. Are you using a multi-module project? Commented Feb 10, 2017 at 7:27
  • Yep it is multi-module maven project...but that is not my concern... Commented Feb 10, 2017 at 7:36

1 Answer 1

0

We are also defining 2 separate variables in our pom, containing the path to the jacoco.exec we want to see generated for normal tests and integration tests:

<sonar.jacoco.reportPath>${project.basedir}/../../target/jacoco.exec</sonar.jacoco.reportPath>
<sonar.jacoco.itReportPath>${project.basedir}/../../target/jacoco-it.exec</sonar.jacoco.itReportPath>

We have 2 seperate profiles (hooking into prepare-agent and prepare-agent-integration respectively):

<profile>
        <id>jacoco-ut</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>${jacoco.version}</version>
                    <executions>
                        <execution>
                            <id>agent-for-ut</id>
                            <configuration>
                                <destFile>${sonar.jacoco.reportPath}</destFile>
                                <append>true</append>
                            </configuration>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

    <!-- Code coverage with Jacoco on integration-tests -->
    <profile>
        <id>jacoco-it</id>
        <build>
            <plugins>
               <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>${jacoco.version}</version>
                    <executions>
                        <execution>
                            <id>agent-for-it</id>
                            <configuration>
                                <destFile>${sonar.jacoco.itReportPath}</destFile>
                                <append>true</append>
                            </configuration>
                            <goals>
                              <goal>prepare-agent-integration</goal>
                            </goals>
                          </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

This is how we run both unit tests and integration-tests and finally send results to sonar:

mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=true -Pdefault,jacoco-ut
mvn org.jacoco:jacoco-maven-plugin:prepare-agent-integration install -Pintegration-tests,jacoco-it -Dmaven.test.failure.ignore=true
mvn sonar:sonar -Pdefault -Dsonar.host.url=http://XX -Dsonar.dynamicAnalysis=reuseReports  -Dmaven.test.skip=true -Dmaven.test.failure.ignore=true -DsonarPassword=XX -DsonarJdbcPassword=XX -Psonar

So we call prepare-agent install using the jacoco-ut profile and the prepare-agent-integration install using the jacoco-it profile.

Sign up to request clarification or add additional context in comments.

2 Comments

I think, you run the intergration-test / junit test, For me, those are already ran somewhere else in some other job(parallel), I don't want to run them here, i just want to get the jacoco.exec file, and then seed them to sonar. Just some performance improvement I don't want to run them again, do you think this can be achieved?
So Just to update :- So here is how I fixed the issue. The sonar server version we are running updated to 5.5(after that version Java-8 is required), i was able to seed 2 jacoco files, one for junit and one for integration-test. It generates same code coverage in sonarquebe as previous.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.