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

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.

Related

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.

Unable to see Junit cobertura coverage reports in SonarQube

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>

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)

Sonar and Jacoco : How to ? (Compatibility with Cobertura)

I know there are lot of questions about Sonar and Jacoco not generating the report correctly on SO but none of them helped me solve my problem:
Here is the deal :
I have a project using Cobertura to calculate code coverage and I can't change that. Sonar must use Cobertura for the server-side. But on the other hand I have this client (we're talking about a webapplication using smartGWT in java) which is tested with Selenium RC (because that's the only way to test smartGWT).
Cobertura can't calculate Selenium tests' coverage because those tests are done on the deployed application. Jacoco can calculate that because it is launched as a java agent. So everything is working fine on my computer. The jacoco.exec is generated successfully. But it seems that Sonar can't find how to read that file or maybe it's just cobertura and jacoco that are conflicted...
Here is the error that Jenkins generate :
[INFO] Surefire report directory: D:\JENKINS-SONAR\jobs\agepro PROTO\workspace\target\surefire-reports
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:323)
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:338)
Caused by: java.io.FileNotFoundException: D:\JENKINS-SONAR\jobs\agepro (Accès refusé)
at java.io.FileOutputStream.openAppend(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:177)
at org.jacoco.agent.rt_9w37ty.controller.LocalController.startup(LocalController.java:46)
at org.jacoco.agent.rt_9w37ty.JacocoAgent.init(JacocoAgent.java:83)
at org.jacoco.agent.rt_9w37ty.JacocoAgent.premain(JacocoAgent.java:165)
... 6 more
FATAL ERROR in native method: processing of -javaagent failed
Exception in thread "main"
And here is my configuration of the surefire plugin :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-javaagent:${sonar.jacoco.jar}=destfile=${sonar.jacoco.reportPath}</argLine>
<excludes>
<exclude>**/Selenium*.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>surefire-integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<!-- <argLine>-javaagent:${sonar.jacoco.jar}=destfile=${sonar.jacoco.reportPath}</argLine> -->
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/Selenium*.java</include>
</includes>
<goal>selenium-test</goal>
</configuration>
</execution>
</executions>
</plugin>
Do you see any mistake I could have made ? Or maybe I have to set another path for the jacoco jar or something else ? Thanks for your help anyway.
The name reportPath is a bit misleading.
The setting sonar.jacoco.reportPath needs to point to a file and not to a directory.
You seem to have set it to: D:\JENKINS-SONAR\jobs\agepro: But I think you mean D:\JENKINS-SONAR\jobs\agepro\jacoco.exec
On further inspection I noticed that the problem might be that you working directory "agepro PROTO" has a space in it.
This breaks the syntax for the java agent configuration. Can you get rid of the space(I don't know if destfile='${sonar.jacoco.reportPath}' would work)

Resources