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

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>

Related

I am getting an error "Error generating the report: java.lang.NullPointerException" while running the JMeter script using JMeter-Maven plugin

I am getting an error "Error generating the report: java.lang.NullPointerException" while running the JMeter script using JMeter-Maven plugin.
Below is the screen shot of the error I am getting:
Below is my pom.xml file:
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.branch.performance
simplifiedAccess
0.0.1-SNAPSHOT
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--Access Portal Login page URL -->
<AccessLoginPageURL>test-login.com</AccessLoginPageURL>
<!-- base URL -->
<BaseURL>test.branch.com</BaseURL>
<!-- Number of users to simulate to execute the performance test plan -->
<numberOfUsers>3</numberOfUsers>
<!-- Time in seconds to get all users activated -->
<rampupTimeInSeconds>1</rampupTimeInSeconds>
<!-- Number of times the plan executed by every user -->
<numberOfLoops>1</numberOfLoops>
<apdexSatisfiedThreshold>2000</apdexSatisfiedThreshold>
<apdexToleratedThreshold>4000</apdexToleratedThreshold>
<!-- JMeter Dashboard output file report name -->
<jmeterReportTitle>Performance Dashboard</jmeterReportTitle>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>configuration</id>
<goals>
<goal>configure</goal>
</goals>
</execution>
<execution>
<id>jmeter-tests</id>
<phase>verify</phase>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
<execution>
<id>jmeter-check-results</id>
<goals>
<goal>results</goal>
</goals>
</execution>
</executions>
<configuration>
<junitLibraries>
<artifact>mysql:mysql-connector-java:8.0.18</artifact>
</junitLibraries>
<testFilesIncluded>
<jMeterTestFile>MyTestFile.jmx</jMeterTestFile>
</testFilesIncluded>
<testResultsTimestamp>false</testResultsTimestamp>
<propertiesUser>
<AccessLoginPageURL>${LoginPageURL}</AccessLoginPageURL>
<BaseURL>${BaseURL}</BaseURL>
<numberOfUsers>${numberOfUsers}</numberOfUsers>
<rampupTimeInSeconds>${rampupTimeInSeconds}</rampupTimeInSeconds>
<numberOfLoops>${numberOfLoops}</numberOfLoops>
<jmeter.reportgenerator.apdex_satisfied_threshold>${apdexSatisfiedThreshold}</jmeter.reportgenerator.apdex_satisfied_threshold>
<jmeter.reportgenerator.apdex_tolerated_threshold>${apdexToleratedThreshold}</jmeter.reportgenerator.apdex_tolerated_threshold>
<jmeter.reportgenerator.report_title>${jmeterReportTitle}</jmeter.reportgenerator.report_title>
<!-- if below is true then doesn't show summary of events in console log -->
<summariser.ignore_transaction_controller_sample_result>false</summariser.ignore_transaction_controller_sample_result>
<jmeter.save.saveservice.subresults>true</jmeter.save.saveservice.subresults>
<!-- Generate JMeter report with only transaction labels and data without sub samplers -->
<!--jmeter.reportgenerator.exporter.html.show_controllers_only>true</jmeter.reportgenerator.exporter.html.show_controllers_only-->
</propertiesUser>
<generateReports>true</generateReports>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</reporting>
Your test didn't execute successfully:
therefore results file is empty therefore JMeter fails to generate the HTML Reporting Dashboard.
Actually a not-properly-handled NPE indicates that a big problem in the code as yoiu shouldn't normally see issues like this in more or less mature software so you can report the issue via JMeter Bugzilla as JMeter shouldn't throw a NPE, it should rather report a human-readable message that the results file is empty suggesting inspecting jmeter.log file for possible reasons.
So you should see your_test_script_name.jmx.log file under target/jmeter/logs folder., I'm pretty much sure that you will find the root cause of your test script failure there.
Check out How to Use the JMeter Maven Plugin article for more information on running JMeter tests via Apache Maven
Run the test from GUI Mode once and confirm that test is getting executed properly. then run from the CLI Mode.

Sonar/Jacoco measured test coverage degrades by every commit for no obvious reason

I have a SpringBoot Maven multi module project with sonar integration configured as follows:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
...
<sonar.projectKey>${project.artifactId}</sonar.projectKey>
<sonar.projectName>${project.artifactId}</sonar.projectName>
<sonar.sources>src/</sonar.sources>
<sonar.tests>src/test/</sonar.tests>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.jacoco.reportPaths>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPaths>
<sonar.language>java</sonar.language>
<sonar.exclusions>**/test/**/*</sonar.exclusions>
</properties>
...
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<configuration>
<destFile>${sonar.jacoco.reportPaths}</destFile>
<append>true</append>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<output>file</output>
<destFile>${sonar.jacoco.reportPaths}</destFile>
</configuration>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${sonar.jacoco.reportPaths}</dataFile>
<outputDirectory>${project.basedir}/../target/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Now I run into the strange phenomenon that the test coverage of this project (according to JaCoCo) degrades over time. Commits changing just one line (which was covered before and is covered after) can result in a 0.1% lower coverage.
This began after a refactoring of the module structure; before each submodule had SpringBoot as parent, while it was recorded as child of the (real) parent module. I changed that so that now each module has the parent module as parent and gets SpringBoot inserted via maven plugin. This works well so far; only after that the count of unit tests sank by five; whereas test from every module are recorded. I could not yet identify which tests are missing as we we have several hundreds of them in this project.
And (as mentioned) with every code change the coverage detected degrades. Before the module refactoring a coverage of nearly 80% was shown; now it has sunken to 26% without much code added or tests removed.
Update:
I have found some warnings in the build log:
[WARNING] The following class(es) did not match with execution data:
...
[WARNING] In order to have accurate coverage measures, the same class files must be used as at runtime for report generation.
[INFO] No information about coverage per test.
Given is a list of some few classes, but by far not all. And they are from any submodule.
What have I done wrong?
How could I have used another compiler/build chain or whatever for some classes? These are not instrumented by another agents.

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>
....

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.

No test coverage in sonar

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.

Resources