Unable to see Junit cobertura coverage reports in SonarQube - maven

SonarQube is reporting the following for my project.
Unit Tests Coverage 0.0%; Line Coverage 0.0%; Condition Coverage 0.0%
It is not able to find the reports which was created immediately before the Sonar scan. I am using Jenkins to launch the SonarQube scan. In fact in the console output I can see the unit tests being executed and the reports saved in the surefire directory.
Below are the relevant logs from the console output.
[INFO] --- maven-surefire-plugin:2.12:test (default-test) # earn-promotional-domain ---
[INFO] Surefire report directory: /var/jenkins/workspace/earn/surefire-reports
[INFO] [13:50:20.807] Sensor SurefireSensor
[INFO] [13:50:20.807] parsing /var/jenkins/workspace/earn/surefire-reports
[ERROR] [13:50:20.808] Reports path not found or is not a directory: /var/jenkins/workspace/earn/surefire-reports
[INFO] [13:50:20.808] Sensor SurefireSensor (done) | time=1ms
[INFO] [13:50:20.808] Sensor CoberturaSensor
[INFO] [13:50:20.808] parsing /var/jenkins/workspace/earn/site/cobertura/coverage.xml
I am using SonarQube 5.1.2. Please let me know if any other information is needed that will help to figure out the problem.

You are better off with Jacoco than cobertura. Cobertura has lot of open bugs yet to be addressed.
Also Jacoco provides a aggregator plugin which will aggregate the coverage from all child modules and display a comprehensive coverage.
Jacoco also doesnt require any additional plugin for SonarQube.
Here's a good example of implementing Jacoco:
http://www.petrikainulainen.net/programming/maven/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin/

If you prefer to stick with Cobertura rather than moving to Jacoco, you can try this alternative to cobertura maven plugin:
https://github.com/QualInsight/qualinsight-mojo-cobertura
Here are some advantages this mojo provides:
it runs tests once (instead of twice with cobertura-maven-plugin)
it allows you to split coverage (UT / IT / Overall)
you get rid of the "obsolete" Cobertura plugin for SonarQube
The documentation on the project's page explains how to add converted reports to SonarQube.

As #Jeel said, you can use an alternative plugin. But if you necessarily must use the cobertura plugin, you can modify it a little bit.
If I understand correctly, the cobertura:check goal forks a current lifecycle, merges a plugin specific configuration (cobertura-maven-plugin-X.X.jar\META-INF\maven\lifecycle.xml) and executes a forked lifecycle to the test phase. After that "check" mojo is executed.
To make cobertura not to fork a lifecycle you can comment <executePhase> tag in a plugin descriptor (cobertura-maven-plugin-X.X.jar\META-INF\maven\plugin.xml) for the check goal.
Additionaly, you have to copy the configuration from lifecycle.xml to your build configuration. For version 2.7 there is only surefire configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<classesDirectory>${project.build.directory}/generated-classes/cobertura</classesDirectory>
<testFailureIgnore>false</testFailureIgnore>
</configuration>
</plugin>
(possibly extending a default lifecycle using components.xml would also work).
In the last step you have to add an execution for the "instrument" goal in the "process-class" phase because it doesn't have a default phase in a plugin descriptor.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>${cobertura.version}</version>
<executions>
<execution>
<id>instrument-classes</id>
<phase>process-classes</phase>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>check-coverage</id>
<goals>
<goal>clean</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>

Related

All maven surefire reports are zero! How could this happen?

I have a scala project configured with maven surefire plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
When I run all the tests and generates the test report, I found the following information on the index page:
Surefire Report
Summary
[Summary] [Package List] [Test Cases]
Tests Errors Failures Skipped Success Rate Time
0 0 0 0 0% 0
It appears that all scalatest result under test-results/scalatest are ignored! How could this happen? This will never happen to gradle test report.
I just found out that scalatest was not designed to work with either maven-surefire-plugin or maven-surefire-report-plugin, it has to generate its own test report in html format:
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<reportsDirectory>${project.build.directory}/test-results/scalatest</reportsDirectory>
<junitxml>.</junitxml>
<htmlreporters>${project.build.directory}/site/scalatest</htmlreporters>
...
case closed.

How to regenerate the source spring boot server-code from swagger-codegen-maven-plugin with modified swagger definition

My requirement is that I have to generate the springboot server code from a swagger definition. I have generated the code with the help of below-mentioned command (by using swagger-codegen-cli-2.3.1.jar).
java -jar swagger-codegen-cli-2.3.1.jar generate ^
-i nycemoves.yml ^
--api-package com.nyce.moves.api ^
--model-package com.nyce.moves.model ^
--invoker-package com.nyce.moves.invoker ^
--group-id com.nyce.moves ^
--artifact-id nyce-moves ^
--artifact-version 0.0.1-SNAPSHOT ^
-l spring ^
--library spring-boot ^
-o nyce-moves
Now, we have updated the swagger definition and want to re-generate the resulting models and api-invokers, for this we would like to use a maven plugin.
After looking through various answers on the internet, we came across the following build plugins which we have added in our pom.xml.
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>src/main/resources/nycemoves.yml</inputSpec>
<language>spring</language>
<basePackage>${default.package}</basePackage>
<modelPackage>${default.package}.model</modelPackage>
<apiPackage>${default.package}.api</apiPackage>
<invokerPackage>${default.package}.invoker</invokerPackage>
<configOptions>
<interfaceOnly>true</interfaceOnly>
<dateLibrary>java8</dateLibrary>
<java8>true</java8>
<library>spring-boot</library>
<serializableModel>true</serializableModel>
<sourceFolder>src/main/java</sourceFolder>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Now, when we do mvn clean package, we want it to update / override all the models, api and invokers. But, this is not working. We are seeing the below mentioned exception.
[INFO] 16 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.516 s
[INFO] Finished at: 2019-03-06T15:05:58+05:30
[INFO] Final Memory: 34M/448M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project nyce-moves: Compilation failure: Compilation failure:
[ERROR] /nyce-moves/src/main/java/com/nyce/moves/api/CommentsApi.java:[29,8] duplicate class: com.nyce.moves.api.CommentsApi
[ERROR] /nyce-moves/target/generated-sources/swagger/src/main/java/com/nyce/moves/model/PostRequest.java:[19,8] duplicate class: com.nyce.moves.model.PostRequest
...
So, we actually need the plugin to do the following things, but we are not able to figure it out.
a) When we run mvn clean package it should regenerate all the source-code for the server stub in source folder as well as target folder.
b) In case of the class already exists, it should override them. We would like to handle the override later with the help of .swagger-codegen-ignore.
I know there are lots of discussion threads on this on stackoverflow, but I could not find any solution which can help me here. Please help me on this or direct me to the relevant thread.
I had the same problem and I needed to define also the output directory. Here is a snippet:
<configuration>
<language>spring</language>
<library>spring-cloud</library>
<configOptions>
<sourceFolder>swagger</sourceFolder>
<interfaceOnly>true</interfaceOnly>
<dateLibrary>java8</dateLibrary>
</configOptions>
<output>${project.build.directory}/generated-sources</output>
</configuration>

jacoco as surefire argLine - The command line is too long

I'm running Jacoco as an agent to surefire
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<argLine>${jacoco.agent.argLine}</argLine>
</configuration>
</plugin>
I'm configuring Jacoco with a large list of exclusions (constants and my deprecated classes) for my build-breaker.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<configuration>
<excludes>
<exclude>*/*Test</exclude>
<exclude>*/*Constants*</exclude>
<exclude>${jacoco.exclusions.list}</exclude>
</excludes>
</configuration>
What I'm getting is that when I go to run the tests I get the error:
T E S T S
-------------------------------------------------------
The command line is too long.
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
Is there another way to pass the exclusions list to Jacoco than on the agent parameter arglist on the command line? (Looking at the code it doesn't look like it)
My question is: How to pass the list of exclusions to the Jacoco agent running in surefire other than on the command line?
As of today JaCoCo Java Agent doesn't allow reading of options from files.
However you can specify agent via environment variable JAVA_TOOL_OPTIONS, which supposed to have bigger size limit than command line.
Also JaCoCo in offline mode (without agent) allows to provide configuration via file jacoco-agent.properties.

JaCoCo - SonarQube - No information about coverage per test

I'm using JaCoCo for Code Coverage. The Unit Test reports are created with junit and they are imported correctly, so that the unit test information is shown properly.
The problem is, that I get the error message:
No information about coverage per test. and the code coverage is shows the value 0% for unit tests, integration tests and overall coverage.
I checked all required information in the sonar-project.properties like binary, src, tests etc.
I'm using:
- SonarQube 4.5.1
- SonarRunner 2.4
- MySQL
- junit 4.1.1
- jacoco 0.7.2
The jacoco.exec is located in a file /target in the project base directory.
Following you can see the sonar-project.properties:
From my point of view all necessary paths are set properly. (i.e. binary, src, tests)
Comma-separated paths to directories with sources (required)
sonar.sources=src
compiled code
sonar.java.binaries=class
source code of unit tests
sonar.tests=test/src
Comma-separated paths to files with third-party libraries (JAR files in the case of Java)
sonar.java.libraries=jar
Language
sonar.language=java
Encoding of the source files
sonar.sourceEncoding=UTF-8
Additional parameters
sonar.my.property=value
Set Project Base
sonar.projectBaseDir=C:/snapshots/steffen_latest/software/java
Tells SonarQube to reuse existing reports for unit tests execution and coverage reports
sonar.dynamicAnalysis=reuseReports
JUnit path
sonar.surefire.reportsPath=test/report/junit
Tells SonarQube where the unit tests execution reports are
sonar.junit.reportsPath=test/report/junit
Tells SonarQube that the code coverage tool by unit tests is JaCoCo
sonar.java.coveragePlugin=jacoco
Import JaCoCo code coverage report.
Tells SonarQube where the unit tests code coverage report is
Unit Tests Coverage
sonar.jacoco.reportPath=target/jacoco.exec
Tells SonarQube where the integration tests code coverage report is
sonar.jacoco.itReportPath=target/it-jacoco.exec
This is the logging file from sonar-runner:
13:56:05.883 INFO - Sensor SurefireSensor...
13:56:05.883 INFO - parsing C:\work\snapshots\steffen_latest\software\java\test\report\junit
13:56:06.149 INFO - Sensor SurefireSensor done: 266 ms
13:56:06.149 INFO - Sensor JaCoCoItSensor...
13:56:06.195 INFO - Analysing C:\work\snapshots\steffen_latest\software\java\target\it-jacoco.exec
13:56:06.726 INFO - **No information about coverage per test**.
13:56:06.726 INFO - Sensor JaCoCoItSensor done: 577 ms
13:56:06.726 INFO - Sensor JaCoCoOverallSensor...
13:56:06.851 INFO - Analysing C:\work\snapshots\steffen_latest\software\java\.sonar\jacoco-overall.exec
13:56:07.178 INFO - **No information about coverage per test**.
13:56:07.178 INFO - Sensor JaCoCoOverallSensor done: 452 ms
13:56:07.178 INFO - Sensor JaCoCoSensor...
13:56:07.209 INFO - Analysing C:\work\snapshots\steffen_latest\or_base\software\java\target\jacoco.exec
13:56:07.521 INFO - **No information about coverage per test**.
13:56:07.521 INFO - Sensor JaCoCoSensor done: 343 ms
13:56:07.521 INFO - Sensor CPD Sensor (wrapped)...
13:56:07.521 INFO - JavaCpdEngine is used for java
13:56:07.521 INFO - Cross-project analysis disabled
13:56:09.019 INFO - Sensor CPD Sensor (wrapped) done: 1498 ms
13:56:09.144 INFO - Execute decorators...
13:56:16.166 INFO - Store results in database
Could anyone give me an advice what could be the problem?
Since I don't know what is the problem...
I'm working on this issues since a few days and I really don't know what to do..
Thank you in advance.
Have you tried using the prepare-agent?
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install
Also, if your coverage keeps showing 0%, you might need to follow this advice:
If your project already uses the argLine to configure the surefire-maven-plugin, be sure that argLine defined as a property, rather than as part of the plugin configuration."
With this Maven configuration I am able to see coverage per test data.
You need to configure sonar-jacoco-listeners to get coverage per test.
Please be aware that it is deprecated by sonar: "this feature is deprecated at SonarQube level and will no longer receive further improvements/maintenance."
<skipTests>false</skipTests>
<!--Jacoco settings -->
<jacoco.haltOnFailure>false</jacoco.haltOnFailure>
<jacoco.skip>false</jacoco.skip>
<!-- sonar JACOCO properties -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.jacoco.reportPaths>${project.reporting.outputDirectory}/jacoco-ut.exec</sonar.jacoco.reportPaths>
<sonar.language>java</sonar.language>
<!-- Added for Jacoco -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<properties>
<property>
<name>listener</name>
<value>org.sonar.java.jacoco.JUnitListener</value>
</property>
</properties>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<configuration>
<destFile>${sonar.jacoco.reportPaths}</destFile>
<append>true</append>
</configuration>
<executions>
<execution>
<id>agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>0.8.1</version>
<classifier>runtime</classifier>
</dependency>
<dependency>
<groupId>org.codehaus.sonar-plugins.java</groupId>
<artifactId>sonar-jacoco-listeners</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
In my case below commands works.
mvn clean org.jacoco:jacoco-maven-plugin:0.7.3.201502191951:prepare-agent install
mvn sonar:sonar
To check code coverage: Start SonarQube server -> Run above two commands one after another & you will see code coverage in SonarQube Client.
FYI: My SonarQube Version - 5.1.2. You can download latest version from SonarQube Download
I use JUnit as well and in my case the issue was because of having TestNG dependency in my pom.xml. After removing this unnecessary dependency, everything started to work as expected.

maven-pmd-plugin external custom ruleset doesn't work

I'm using this in my pom in the reporting section:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>2.7.1</version>
<configuration>
<rulesets>
<ruleset>http://serverxxx/pmd-java.xml</ruleset>
</rulesets>
<targetJdk>1.6</targetJdk>
</configuration>
</plugin>
somehow it doesn't work. When I use the rules directly instead of a custom ruleset via http it work fine. I use pmd:pmd and the section is also in the build section of the pom...
Logs:
16:11:05 [INFO] --- maven-pmd-plugin:2.7.1:cpd (default-cli) # online-news ---
16:11:05 mojoSucceeded org.apache.maven.plugins:maven-pmd-plugin:2.7.1(default-cli)
16:11:05 [DRY] Finding all files that match the pattern cpd.xml
16:11:05 [DRY] Parsing 1 files in D:\build\hudson\jobs\xxx migration-maven-3 CI\workspace\migration-maven-3\application\online-modules\online-news\target
16:11:06 [DRY] Successfully parsed file D:\build\hudson\jobs\xxx migration-maven-3 CI\workspace\migration-maven-3\application\online-modules\online-news\target\cpd.xml of module News with 31 warnings.
16:11:06 [DRY] Computing warning deltas based on reference build #13
16:11:06 mojoStarted org.codehaus.mojo:findbugs-maven-plugin:2.5.2(default-cli)

Resources