No test coverage in sonar - maven

I have a multimodule maven project. I have 2 tests in it. Tests run during build and sonarqube shows that 100% tests (2) passed. But coverage is empty. Why is it? My other simple maven project uploads coverage successfully in sonarqube. I made the test in the same way in both projects.

There are probably more ways to do it, but the best way I found is to use the Jacoco maven plugin, configure it to add it's agent to the Surefire execution (and, optionally, if you use integration tests, Failsafe) and then add these files to the SonarQube config.
A good explanation for this I've found here. The basic stuff is quite simply (here only for surefire):
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<executions>
<!--
Prepares the property pointing to the JaCoCo runtime agent which
is passed as VM argument when Maven the Surefire plugin is executed.
-->
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<!--
Sets the name of the property containing the settings
for JaCoCo runtime agent.
-->
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<!--
Ensures that the code coverage report for unit tests is created after
unit tests have been run.
-->
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<configuration>
<!-- Sets the VM argument line used when unit tests are run. -->
<argLine>${surefireArgLine}</argLine>
<!-- Skips unit tests if the value of skip.unit.tests property is true -->
<skipTests>${skip.unit.tests}</skipTests>
</configuration>
</plugin>
With that you can configure your SonarQube (Settings -> Java) to find the jacoco unittest file at /target/coverage-reports/jacoco-ut.exec, which should be enough to get the coverage reliably when running the sonar:sonar goal afterwards.

Related

Getting “Skipping JaCoCo execution due to missing execution data file” in local

I have the below pom.xml which is fine.I am able to generate jacoco.exec and see the coverage in sonarqube.But the problem is when i am run the code in local (my laptop). target/site folder is not generated and jacoco reports are not generated jacoco.exe is generated is starting folder of my app and not in target folder.
How can i solve this issue for local ?
I get the below error when i run maven install or maven test
Skipping JaCoCo execution due to missing execution data file
I have maven spring boot application
pom.xml
<properties>
<!-- Sonar -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
<sonar.language>java</sonar.language>
</properties>
-- snipper of jacoco plugin tag
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
</configuration>
Quite a long time ago, we replaced the use of the "jacoco.exec" file with the use of the "jacoco.xml" file. The former is supposedly deprecated now.
We set the following properties:
sonar.coverage.jacoco.xmlReportPaths: ${basedir}/target/jacoco_report/jacoco.xml
jacoco.path: ${basedir}/target/jacoco_report
In the jacoco plugin configuration, have the following:
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${sonar.jacoco.reportPath}</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<!-- Ensures that the code coverage report for unit tests is created
after unit tests have been run. -->
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${sonar.jacoco.reportPath}</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${jacoco.path}</outputDirectory>
</configuration>
</execution>
In the surefire plugin configuration, we have this:
<argLine>${surefireArgLine}</argLine>

Using sonar.test.exclusions with Sonarqube 6.3

I'm currently evaluating Sonarqube 6.3 (a big upgrade from my current 5.5 instance) and I'm getting confused trying to work out the functionality of the sonar.test.exclusions setting.
There's this question: Sonar Maven Plugin: How do I exclude test source directories? which seems to indicate that it is used to exclude test files from analysis (which is what I'm after - I don't want my sonar ruleset run over my unit tests). The documentation https://docs.sonarqube.org/display/SONAR/Narrowing+the+Focus also indicates that it is used to 'exclude unit test files' (perhaps this can be expanded upon to make it clearer?)
Thing is, when I add sonar.test.exclusions with a value of **/src/test/** and then run my analysis, I'm still getting code smells and the like being found for:
Foo/src/test/java/foo/bar/BarTest.java
Foo/src/test/java/lah/LahTest.java
etc.
When I use sonar.exclusions instead, they don't show up. Why is sonar.test.exclusions not doing what I expect?
First of all: if you have a Maven project, you should use the scanner for Maven (mvn sonar:sonar). It will simplify your configuration, and will automatically register src/test/java folder as a test directory.
Now if you want to do the configuration manually, or understand what is going on under the hood, here is the explanation: SonarQube scanner work with 2 sets of files, main and test. Main source files are configured using the property sonar.sources. Test source files are configured using sonar.tests.
On top of that, you can filter some content using the sonar.[test.]exclusions properties.
In you case your problem is that Foo/src/test/java/foo/bar/BarTest.java seems to be considered as a main source file. That's why sonar.test.exclusions has no effect.
Using maven with verfication goal (mvn clean verify sonar:sonar install), I have used this configuration without problems:
...
<properties>
....
<sonar.exclusions>
**/generated/**/*,
**/model/**/*
</sonar.exclusions>
<sonar.test.exclusions>
src/test/**/*
</sonar.test.exclusions>
....
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
<sonar.coverage.exclusions>
**/generated/**/*,
**/model/**/*
</sonar.coverage.exclusions>
<jacoco.version>0.7.5.201505241946</jacoco.version>
....
</properties>
....
Coverage exclusion configuration, inside properties (up) and jacoco plugin configuracion:
.....
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>target/jacoco.exec</dataFile>
<outputDirectory>target/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
....

Can we execute exec-maven-plugin before maven-surefire-plugin?

Is it possible to run exec-maven-plugin before maven-surefire-plugin, what I observe during my run is maven-surefire-plugin is executing first even though the sequence in tag is second. My scenario is to execute JAVA CLASS (using exec-maven-plugin ) which generates my testng.xml and can run that using (maven-surefire-plugin).
First of all, if you have an execution of the exec-maven-plugin bound to the test phase, it is normal that this execution is performed after the one of the maven-surefire-plugin. The reason is that you're likely dealing with a project of packaging jar, which has a default binding of the Surefire Plugin to the test phase. This default execution is always the first one invoked, regardless of where the plugin is declared in the POM. In the logs, you will spot this execution with an id of default-test.
There is a way to perform actions before the tests are run by leveraging the phases invoked before the phase test. In your case, your goal is to generate a test resource, the testng.xml, so it would be appropriate to use the generate-test-resources phase, whose purpose is to create resources that are needed for the tests. Therefore, you just need to specify
<phase>generate-test-resources</phase>
to the execution of the exec-maven-plugin generating the testng.xml.
Then, you can use the generated testng.xml with the suiteXmlFiles element, see Using Suite XML Files
This is how I have implemented:
I have added test script/java main class, I want to execute before Cucumber Test Suite, in following folder:
enter image description here
Added following in POM.xml in ...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/java/BeforeSuite</source> <!-- source folder where Before Suite scripts are saved -->
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>before-test-suite-scripts</id>
<phase>generate-test-resources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>BeforeSuite.HelloBeforeSuiteScript</mainClass> <!-- <packagename>.<className> -->
</configuration>
</execution>
</executions>
</plugin>
When running mvn clean verify, before test suite script will run prior the test suite execution.
enter image description here

How to measure Code Coverage of a RestFul API

I have a maven based multi-module Jersey project which exposes few RestFul API. Project Structure is like -
Project
Module1
----------src
--------- unit test
Module2
----------src
--------- unit test
Module3
----------src
--------- unit test
ModuleN - this module contains Integration Tests which will hit endpoints exposed by project and test the whole service like a black box
----------src
--------- unit test
I want to build this project and execute unit tests in during build phase then create a jar, deploy this jar somewhere, execute integration tests (which is present in one module of project and this will hit the REST end points) then I wanted to measure combine coverage (unit+integration tests).
I have gone though a lot of blogs and articles but everywhere we have half information. can someone point me or guide me how can I do it.
Thanks
-Shahid
As I've answered here: You can put all the reports together in one folder (don't forget to call the different!) and use the merge mojo for that, or use a central unique file to all your reports by adding the flag "append":
-javaagent:append=true,destFile=/home/YourProject/report.exec
[Here][2] you'll find more information about how to configure the agent.
Hope it helps!
(this is a copy of my other answer to the same problem)
EDIT:
As long as you were asking for different kind of execution (unit and integration) I assume you are using different plugins for the execution of your tests.
What you have to do is to prepare two JaCoCo agents and give the argLine of them to the plugin that will run your test:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<executions>
<!--
Prepares the property pointing to the JaCoCo runtime agent which
is passed as VM argument when Maven the Surefire plugin is executed.
-->
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${project.build.directory}/jacoco.exec</destFile>
<!--
Sets the name of the property containing the settings
for JaCoCo runtime agent.
-->
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<!--
Ensures that the code coverage report for unit tests is created after
unit tests have been run.
-->
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}</outputDirectory>
</configuration>
</execution>
<!-- The Executions required by unit tests are omitted. -->
<!--
Prepares the property pointing to the JaCoCo runtime agent which
is passed as VM argument when Maven the Failsafe plugin is executed.
-->
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${project.build.directory}/jacoco-it.exec</destFile>
<!--
Sets the name of the property containing the settings
for JaCoCo runtime agent.
-->
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
<!--
Ensures that the code coverage report for integration tests after
integration tests have been run.
-->
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Then you have to configure your plugins (normally used failsafe and surefire) for accepting the argLine created for the JaCoCo agents:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${surefireArgLine}</argLine>
</configuration>
</plugin>
The same for the failsafe plugin.

Getting "Skipping JaCoCo execution due to missing execution data file" upon executing JaCoCo

I'm using Maven 3.0.3, JUnit 4.8.1, and Jacoco 0.6.3.201306030806, and I am trying to create test coverage reports.
I have a project with unit tests only, but I can't get reports to run, I'm repeatedly getting the error: Skipping JaCoCo execution due to missing execution data file when I run:
mvn clean install -P test-coverage
Here is how my pom is configured:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<reuseForks>true</reuseForks>
<argLine>-Xmx2048m</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<reuseForks>true</reuseForks>
<argLine>-Xmx4096m -XX:MaxPermSize=512M ${itCoverageAgent}</argLine>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
...
<profile>
<id>test-coverage</id>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.3.201306030806</version>
<configuration>
<destfile>${basedir}/target/coverage-reports/jacoco-unit.exec</destfile>
<datafile>${basedir}/target/coverage-reports/jacoco-unit.exec</datafile>
</configuration>
<executions>
<execution>
<id>prepare-unit-tests</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- prepare agent for measuring integration tests -->
<execution>
<id>prepare-integration-tests</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<propertyName>itCoverageAgent</propertyName>
</configuration>
</execution>
<execution>
<id>jacoco-site</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
All my tests run successfully. Here is some of the output from Maven:
[INFO] --- jacoco-maven-plugin:0.6.2.201302030002:prepare-agent (prepare-unit-tests) # myproject ---
[INFO] argLine set to -javaagent:/Users/davea/.m2/repository/org/jacoco/org.jacoco.agent/0.6.2.201302030002/org.jacoco.agent-0.6.2.201302030002-runtime.jar=destfile=/Users/davea/Dropbox/workspace/myproject/target/jacoco.exec
[INFO]
...
Tests run: 14, Failures: 0, Errors: 0, Skipped: 0
[INFO]
...
[INFO]
[INFO] --- jacoco-maven-plugin:0.6.2.201302030002:prepare-agent (prepare-integration-tests) # myproject ---
[INFO] itCoverageAgent set to -javaagent:/Users/davea/.m2/repository/org/jacoco/org.jacoco.agent/0.6.2.201302030002/org.jacoco.agent-0.6.2.201302030002-runtime.jar=destfile=/Users/davea/Dropbox/workspace/myproject/target/jacoco.exec
[INFO]
[INFO] --- maven-failsafe-plugin:2.14.1:integration-test (default) # myproject ---
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
[INFO]
[INFO] --- maven-failsafe-plugin:2.14.1:verify (default) # myproject ---
[INFO] Failsafe report directory: /Users/davea/Dropbox/workspace/myproject/target/failsafe-reports
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
[INFO]
[INFO] --- jacoco-maven-plugin:0.6.2.201302030002:report (jacoco-site) # myproject ---
[INFO] Skipping JaCoCo execution due to missing execution data file
[INFO]
Any ideas what configuration I'm missing?
jacoco-maven-plugin:0.7.10-SNAPSHOT
From jacoco:prepare-agent that says:
One of the ways to do this in case of maven-surefire-plugin - is to
use syntax for late property evaluation:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>#{argLine} -your -extra -arguments</argLine>
</configuration>
</plugin>
Note the #{argLine} that's added to -your -extra -arguments.
Thanks Slava Semushin for noticing the change and reporting in the comment.
jacoco-maven-plugin:0.7.2-SNAPSHOT
Following jacoco:prepare-agent that says:
[org.jacoco:jacoco-maven-plugin:0.7.2-SNAPSHOT:prepare-agent] Prepares a property pointing to the JaCoCo runtime agent that can be
passed as a VM argument to the application under test. Depending on
the project packaging type by default a property with the following
name is set:
tycho.testArgLine for packaging type eclipse-test-plugin and
argLine otherwise.
Note that these properties must not be overwritten by the
test configuration, otherwise the JaCoCo agent cannot be attached. If
you need custom parameters please append them. For example:
<argLine>${argLine} -your -extra -arguments</argLine>
Resulting
coverage information is collected during execution and by default
written to a file when the process terminates.
you should change the following line in maven-surefire-plugin plugin configuration from (note the ${argLine} inside <argLine>):
<argLine>-Xmx2048m</argLine>
to
<argLine>${argLine} -Xmx2048m</argLine>
Make also the necessary changes to the other plugin maven-failsafe-plugin and replace the following (again, notice the ${argLine}):
<argLine>-Xmx4096m -XX:MaxPermSize=512M ${itCoverageAgent}</argLine>
to
<argLine>${argLine} -Xmx4096m -XX:MaxPermSize=512M ${itCoverageAgent}</argLine>
I faced a bit of a different issue that returned the same error.
Skipping JaCoCo execution due to missing execution data /target/jacoco.exec
The truth is, this error is returned for many, many reasons.
We experimented with the different solutions on Stack Overflow, but found this resource to be best. It tears down the many different potential reasons why Jacoco could be returning the same error.
For us, the solution was to add a prepare-agent to the configuration.
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
I would imagine most users will be experiencing it for different reasons, so take a look at the aforementioned resource!
One can also get "Skipping JaCoCo execution due to missing execution data file" error due to missing tests in project. For example when you fire up new project and have no *Test.java files at all.
There might a case where some other argline setup or plugin in pom may be overriding jacoco execution order setup.
argLine set to -javaagent:/Users/davea/.m2/repository/org/jacoco/org.jacoco.agent/0.6.2.201302030002/org.jacoco.agent-0.6.2.201302030002-runtime.jar=destfile=/Users/davea/Dropbox/workspace/myproject/target/jacoco.exec
One of the example
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<forkCount>5</forkCount>
<reuseForks>true</reuseForks>
<argLine>-Dnet.sf.ehcache.disabled=true</argLine>
</configuration>
</plugin>
After getting rid of argLine from these plugins, jacoco started to work normally.
I know this question is pretty old but if someone like me comes here looking for an answer then this might help. I have been able to overcome the above error with this.
1) Remove the below piece of code from the plugin maven-surefire-plugin
<reuseForks>true</reuseForks>
<argLine>-Xmx2048m</argLine>
2) Add the below goal:
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
I've tried all answers but only the following combination of advice has worked for me. Why? I had very specific requirements:
JaCoCo generates report when build is run from command line: mvn clean verify (Maven 3.6.0)
Intellij IDEA (2019.01) runs my tests as well
It all works in presence of another javaagent defined in surefire plugin
Solution - prepend argLine value in surefire configuration with "late replacement" maven property #{...} as explained in surefire FAQ (my fixed configuration)
How do I use properties set by other plugins in argLine?
Maven does property replacement for
${...}
values in pom.xml before any plugin is run. So Surefire would never see the place-holders in its argLine property.
Since the Version 2.17 using an alternate syntax for these properties,
#{...}
allows late replacement of properties when the plugin is executed, so properties that have been modified by other plugins will be picked up correctly.
Failed first try - define jaCoCoArgLine property in prepare-agent goal configuration of jacoco - the scenario failed my second requirement, IntelliJ IDEA couldn't figure out agent for jmockit I use in the project for static method mocking
When using the maven-surefire-plugin or maven-failsafe-plugin you must not use a forkCount of 0 or set the forkMode to never as this would prevent the execution of the tests with the javaagent set and no coverage would be recorded.
ref: https://www.eclemma.org/jacoco/trunk/doc/maven.html
this is my gist
I was having the same issue. I updated the Jacoco version from 0.6 to 0.8 and updated surefire plugin as well. The following command generated Jacoco reports in site/jacoco/ folder:
mvn clean jacoco:prepare-agent install jacoco:report
My maven configurations are as follows:
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.0</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<forkedProcessExitTimeoutInSeconds>60</forkedProcessExitTimeoutInSeconds>
<forkCount>1</forkCount>
</configuration>
</plugin>
</plugins>
Came accross the same problem just now.
I have a class named HelloWorld, and I created a test class for it named HelloWorldTests, then I got the output Skipping JaCoCo execution due to missing execution data file.
I then tried to change my pom.xml to make it work, but the attempt failed.
Finally, I simply rename HelloWorldTests to HelloWorldTest, and it worked!
So I guess that, by default, jacoco only recognizes test class named like XxxTest, which indicates that it's the test class for Xxx. So simply rename your test classes to this format should work!
FWhat tdrury said:
change your plugin configuration into this:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.3.201306030806</version>
<executions>
<!-- prepare agent for measuring integration tests -->
<execution>
<id>prepare-integration-tests</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
</configuration>
</execution>
<execution>
<id>jacoco-site</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
Edit:
Just noticed one important thing, destFile and dataFile seems case sensitive so it's supposed to be destFile, not destfile.
I struggled for days. I tried all the different configurations suggested in this thread. None of them works. Finally, I find only the important configuration is the prepare-agent goal. But you have to put it in the right phase. I saw so many examples put it in the "pre-integration-test", that's a misleading, as it will only be executed after unit test. So the unit test won't be instrumented.
The right config should just use the default phase, (don't specify the phase explicitly). And usually, you don't need to mass around maven-surefire-plugin.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Try to use:
mvn jacoco:report -debug
to see the details about your reporting process.
I configured my jacoco like this:
<configuration>
<dataFile>~/jacoco.exec</dataFile>
<outputDirectory>~/jacoco</outputDirectory>
</configuration>
Then mvn jacoco:report -debug shows it using the default configuration, which means jacoco.exec is not in ~/jacoco.exec. The error says missing execution data file.
So just use the default configuration:
<execution>
<id>default-report</id>
<goals>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
</configuration>
</execution>
And everything works fine.
I know this is late, but just for anyone else who has this issue and nothing seems to fix it (like I had). Make sure that in you pom, your configuration for jacoco inside plugins is not in turn inside pluginManagement. It seems some sort of default for maven is to put the plugins inside pluginManagement. This is almost unnoticeable until you try to add detailed configurations like for jacoco. In order to add these, you need to add them outside of the pluginManagement, otherwise they are effectively ignored. See my poms below for details.
My old pom (that gave the "Skipping jacoco ..." message):
<project>
...
<build>
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
My new pom (that now compiles a working jacoco report):
<project>
...
<build>
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
It happens if the path of your project has blank spaces anywhere, such as:
/home/user/my projects/awesome project
the report is not generated. If that is the case, remove those spaces from directory names, such as:
/home/user/my-projects/awesome-project
or move the project to a directory that doesn't have spaces along the way.
About the plugin configuration, I just needed the basic as below:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Jacoco Execution data file is a jacoco.exec file which is generated when prepare agent goal is running. When It isn't generated or the correct path isn't set in configuration, you will get that error message. Because Jacoco use It to build test coverage. This usually occur when you use jacoco maven plugin and surfire or failsafe. To ensure that the jacoco.exec file is generated, you have to add argline in you pom.xml file, not in the surfire configuration but inside a properties tag in you pom.xml file.
<properties>
<argLine>-Xmx2048m</argLine>
</properties>
The execution says it's putting the jacoco data in /Users/davea/Dropbox/workspace/myproject/target/jacoco.exec but your maven configuration is looking for the data in ${basedir}/target/coverage-reports/jacoco-unit.exec.
I have added a Maven/Java project with 1 Domain class with the following features:
Unit or Integration testing with the plugins Surefire and Failsafe.
Findbugs.
Test coverage via Jacoco.
Where are the Jacoco results? After testing and running 'mvn clean', you can find the results in 'target/site/jacoco/index.html'. Open this file in the browser.
Enjoy!
I tried to keep the project as simple as possible. The project puts many suggestions from these posts together in an example project. Thank you, contributors!
My response is very late but for others users
In your case you have to configure failsafe pluging to use the command line agent configuration saved in itCoverageAgent variable.
For exemple
<configuration>
<argLine>${itCoverageAgent}</argLine>
</configuration>
In your maven configuration, jacoco prepare the command line arguments in prepare-agent phase, but failsafe plugin doesn't use it so there is no execution data file.
Sometimes the execution runs first time, and when we do maven clean install it doesn't generate after that.
The issue was using true for skipMain and skip properties
under maven-compiler-plugin of the main pom File.
Remove them if they were introduced as a part of any issue or suggestion.
In my case, the prepare agent had a different destFile in configuration, but accordingly the report had to be configured with a dataFile, but this configuration was missing. Once the dataFile was added, it started working fine.
I faced similar error because tests execution were skipped.
I ran my build with similar system parameter : -Dmaven.test.skip.exec=true
Turning this system parameter to false solved the issue.
I just removed following two lines from properties tag
<jacoco.ut.reportPath>${project.basedir}/../target/jacoco.exec</jacoco.ut.reportPath>
<sonar.jacoco.reportPaths>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPaths>
mvn install -Psonar by default creates jacoco.exec in target directory, so explicit path was not needed in my case.
For others that met similar problem, here is another possible solution:
Our project was running in a Traditional Chinese version of Windows, and when we checked prepare-agent section in maven log, we found that it tried to read the project folder which we put on desktop, through \桌面\...\project\... instead of \Desktop\...\project\.... I think UTF-8 symbol in paths make Jacoco go weird. We moved the project into other place and the issue was fixed.
TL;DR:
Check prepare-agent logs too as jacoco.exec was prepared during that.
2 Other Possibilities
JaCoCo's Maven plugin could be better integrated with Maven to provide more information about it's invocation and incorrect invocations. Nonetheless.
Possibility #1: Custom Arguments In Surefire plugin
Using JPMS module for my project with standard Maven directory layout, basic JSE 11 program, JUnit 5 & JaCoCo, with Eclipse and single module-info.java file (Eclipse 4.13 won't allow 2 module-info.java files in the project's root of the classpath). In order for Surefire to see my test cases I had to use the single <argLine> tag to allow Surefire to gain access using: --add-opens for all of the packages having unit tests:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>
#{argLine}
--add-opens module_name/com.myco.project=ALL-UNNAMED
--add-opens module_name/com.myco.project.more=ALL-UNNAMED
--add-opens module_name/com.myco.project.others=ALL-UNNAMED
</argline>
</configuration>
...
</plugin>
According to Jacoco documentation, you have to directly pass the Jacoco arguments to Surefire since specifying any Surefire arguments using <argLine> overrides Jacoco's defaults. Jacoco's online Maven documentation specifies using #{argLine} to pass Jacoco's arguments to Surefire (https://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html).
Possibility #2: Intermingling Plugins
I also use the maven-javadoc plugin in my <reporting/> section. Incidentally, during the Javadoc reporting phase, it manages to invoke the prepare-agent goal of JaCoCo and gives the OP's error message shortly thereafter (according to maven debug, specifically while the maven-project-info-reports-plugin is configuring the reports to begin generating for maven site) - this despite the fact that my Test phase has already run and dumped a proper jacoco.exec file in the build output directory. It may be safe to ignore this warning, my JaCoCo report renders fine in my Maven Site. However, if you're seeing it during the execution of a JaCoCo goal, it probably shouldn't be ignored.
Tips
If you're having doubts about the file getting created, watch the directory where the file is supposed to appear during the build. Generating the file is a fairly slow process. See my other answer here: https://stackoverflow.com/a/75465187/2336934
Do your best to keep to all the defaults as much as possible, simpler is better.

Resources