jacoco code coverage of integration tests in a separate module in Netbeans - java

I have a maven project with integration tests stored in a module called my-project-tests, and production code spread across several other modules. I can get code coverage for the unit tests of individual modules by adding the following plugin instruction to the pom.xml:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.5.7.201204190339</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
I can then view the test report as described in the Netbeans wiki.
When I add this to my integration test module however, the report says "No data -- have you run your code yet?" (I have).
A jacoco.exec file is generated with a lot of data in it, I can't confirm if the data is correct. but a separate jacoco.xml file which Netbeans relies on is left mostly empty.
Is there a way to correctly produce and view code coverage of a multi-module project in Netbeans?

According to this bug, this is not supported in Netbeans: https://netbeans.org/bugzilla/show_bug.cgi?id=223319

Related

Sonar coverage is not calculated

I am trying to integrate my code coverage into the sonar but it shows 0.
I have integrated jacoco as below to my pom but, it still shows the code coverage as zero.
I have only used the pom file from this repository, what other configuration should I work in order to have code coverage ?
What would be other parameters should I possibly add?
https://github.com/daniel-frak/keycloak-user-migration/blob/master/pom.xml
Thanks.
Full repo:
https://github.com/daniel-frak/keycloak-user-migration
There are a number of things that can cause this.
The first thing you should do is examine the workspace to see if the Jacoco plugin wrote its output files anywhere in the "target" tree. It will generate a data file in one or more formats, but it will also generate an html tree, with an "index.html" in the root. You can visit that file, and it will show your coverage results.
If you don't find those files, then either your jacoco plugin is not properly integrated with your surefire plugin, or you simply didn't run your unit tests in the build (don't laugh, I've seen people do this).
The "integration" is done by telling Jacoco what variable to put the required command line into, and telling Surefire to use that variable.
Here is a sample jacoco plugin definition:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.maven.plugin.version}</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<outputDirectory>${jacoco.path}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Note the reference to "surefireArgLine". Here is the Surefire definition that goes with that:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<argLine>${surefireArgLine}</argLine>
</configuration>
</plugin>
We also combine this with these property settings:
<jacoco.path>${basedir}/target/jacoco_report</jacoco.path>
<sonar.coverage.jacoco.xmlReportPaths>${basedir}/target/jacoco_report/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>

jacoco configuration for multi module is not working

There is something strange going on with my jacoco configuration and I am not able to figure it out. I have visited multiple threads on stack overflow and other platforms and tried many thing, but didn't resolve this issue.
I have setup the java code coverage for multiple modules. This is my project structure
ABC
module1
DEF
module1
module2
module3
pom.xml
I have configured the jacoco for my DEF maven project. I am only configuring my DEF project. And this is what pom.xml contains
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/target/reports</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Issue: The issue here is, it's generating the code coverage report in each module1, module2 and module3. But the report generated in module1, doesn't contains the code coverage for itself. Meaning, it shows the code coverage for module2 and module3, but it doesn't include module1 report in itself. I don't know what's wrong ?
EDIT : modules in DEF are maven modules and it didn't contains anything related to jacoco.
Any idea or any suggestions ?
Thanks

Handle different source folder in Eclipse

The Project I am working on has the regular src/main and src/test folders, but they recently introduced src/integration-test, which is supposed to contain, as the name suggests, integration-tests.
Unfortunately I can't seem to make this work with Maven and Eclipse. I tried adding a source folder, but Eclipse is not able to resolve the dependencies needed for the integration-tests and fails to compile the test-classes. I also can't start the tests with JUnit.
The source folder was added to the build path, but it did not help.
All other developers in my team use IntelliJ and report that they don't have these issues at all. But it´s bound to work with Eclipse as well, right?
You should handle this with maven by adding the source folder to the build:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/integration</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
In this example the build will add a src/integration folder where you can put other test classes.
For more information check the build helper plugin documentation.

Multi-module maven jacoco setup for integration tests

I have maven setup with multiple modules, the setup looks something like this
root module
- domain module
- repository module
- service module
- controllers module
Jacoco is correctly generating test coverage from unit tests and sonar is showing the correct percentage (let's say 20%). Surefire is used for unit tests.
For integration tests its more tricky, we use failsafe and integration tests generate jacoco-it.exec file which is scanned by sonar. My problem is that integration tests are located in the controllers module and it only shows test coverage of integration tests on classes that are inside controllers and not on classes that are in another module like service module. Because of this overall test coverage with integration tests increases to something like 21% instead of 35+%.
Is it possible to configure sonar and jacoco to measure test coverage with the integration tests of all classes instead of classes from same module only, if integration tests are in controllers module?
For reference, this is the relevant setup
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
...
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<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>failsafeArgLine</propertyName>
</configuration>
</execution>
</executions>
</plugin>
I run my tests with mvn verify and scanner with mvn sonar:sonar
You can aggregate your coverage reports by writing them all to the same destination file.
For example, we're only covering unit tests in our multi-module projects at the moment, so our parent pom contains:
<properties>
...
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
...
</properties>
and
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<append>true</append>
</configuration>
<inherited>true</inherited>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
This results in a single aggregated report for all modules.
I expect that a similar pattern could be applied to your jacoco IT configuration.

Aggregated Coverage or Coverage in the dependent modules not shown in SonarQube + Reports are generated by Jacoco

I am running jacoco plugin to generate html , xml and jacoco.exec reports to measure the coverage of the code tested by my testNg tests.
I am successful in the generation of these reports in my local as well as in Jenkins and all my unit test results are reflected in Sonar and it's showing me the coverage.
My jacoco.exec has both results of the coverage in the module and the dependent modules. I have verified this using eclemma plugin for eclipse.
I am not getting the coverage results in the dependent modules in Sonar.Does any one what I am doing wrong.
My plugin goes like this
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
and my goal is jacoco:report-aggregate
I got the answer from jacoco plugin coverage in multi-module
The following were the mistakes that I did which caused problem form me. In the properties of our pom
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
and in plugin
<destFile>${sonar.jacoco.reportPath}</destFile>
for me, the above statement flushed the jacoco.exec in different folders because of difference in maven module hierarchy as a result they never agrregated.
The second point is that the dependent module coverage will be only obtained only if it is a compile time dependency to the testing module.

Categories