How to get integration test code coverage using jacoco

We are using sonarqube community version 6.4

  1. We deployed our java-jar application on application server with jacoco jar enabled.
  2. Then after running regression test on the target server, we dumped the jacoco.exec file using below command: java -jar /tmp/lib/jacococli.jar dump --address localhost --port 36320 --destfile /tmp/jacoco-it.exec
  3. It generated the jacoco-it.exe file. Now I want to find out the code coverage of this.
  4. How to upload this exe file to sonarqube to get the coverage?

Thanks,
Chandan

Hello,

First of all I want to tell you that you are running an old sonarqube version which is not supported since a long time. I strongly recommend you to upgrade to a more recent version being 7.9.x or 8.0.

Coming to your question, you can find all you need in the documentation.
We also have a JaCoCo dedicated thread on this community forum.

As you will read in this latest thread, the JaCoCo format we are using now is the XML format, not the binary format any more. The support of XML format come with a much more recent version of the analyser than the one you are probably running.

So in my opinion, your first move would be to upgrade SonarQube and the scanners you are using and then focusing on importing code coverage.

Regards,
Alex.

Hi Alexandre,

Thanks for your quick response. Yeah we are using very old version , the reason for this is that we cannot upgrade java on the current box as multiple services are running on this.

We do have a plan for upgrade.

I already went through both the links earlier before posting here, though I was confused for the below points:

  1. Will the XML generated will have integration test code coverage? As the application is running on other box with jacoco agent jar enabled, I will run the regression/sanity test from Jenkins server/or some other server, how will the XMLs generated on the jenkins will have the sanity/regression code coverage?

  2. Our QA team have written test cases using sure-fire plugin which is generating surefire reports.

I may be very vague or might be asking a basic question, but here is what I am stuck.

Moreover I guess XML support is in recent versions and hence might not work with the version we use.

Also just to make clear, mentioning below the steps I have performed:

  1. We deployed our java-jar application on application server with jacoco jar enabled.
  2. Then after running regression test on the target server, we dumped the jacoco.exec file using below command: java -jar /tmp/lib/jacococli.jar dump --address localhost --port 36320 --destfile /tmp/jacoco-it.exe
  3. It generated the jacoco-it.exe file. Now I want to find out the code coverage of this. [Is this right?]
  4. How to upload this exe file to sonarqube to get the coverage?
  5. I copied the exe file from the application file to the jenkins server, in the path where I have pom.xml of our QA code.
  6. Now for getting the code coverage, I tried below things, added the below content in pom.xml
<profile>
            <id>coverage</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>0.8.3</version>
                        <executions>
                            <execution>
                                <id>prepare-agent</id>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>report</id>
                                <goals>
                                    <goal>report</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

Ran this command:

 mvn clean verify sonar:sonar -Pcoverage  -e -Denvironment=uat2 -DxmlFile=<>  -Dsonar.jacoco.reportPaths=/tmp/jacoco-it.exe  -Dsonar.projectName=<>  -Dsonar.host.url=<> -Dsonar.login=<> -Dsonar.password=<>

It did uploaded on sonar, Is this the right code coverage?

Copying the coverage report from one place to another is not what we recommend to do.
If the paths of the source files do not match in both places the analyser will not be able to match the files referenced in the coverage report and then show no coverage, it is rather by chance that you have coverage after the analysis.
From what I can see on the above message showing the pom.xml jacoco part, you do not need to copy the JaCoCo report like you did as the build should generate it in the target directory.
You can check here for a good example of binary reports usage.

About this,

There is a solution for you to not upgrade the java distribution of the server.
You can install a standalone package of the jdk and target it in the wrapper.conf file of the SonarQube distribution.
Having a more recent version of our products (sonarqube, scanners, analysers) would give you access to more features and to the support of the XML format for the JaCoCo reports.
Alex.

Thanks for bearing my silly question and helping out

With the above way its uploading the jacoco.exec generated but the size of that file is only 20kb ( the process i shared in my earlier mail)

Also after uploading this i am getting only code coverage and the java classes shown in the sonar server analysis is only for the qa automation code base( and not the source code one, application i started with jacoco agent) is this the correct behaviour?

Also not getting the number of integration test cases details also in the sonar server

Hi

One more thing I want to mention is that git repo for application code and test code are separate