I have a multi-module project that I can't seem to get an accurate unit test code coverage report on SonarQube. I use buildr and JaCoCo for the test coverage generation. The file heirarchy is similar to below.
Project
--Module1
----Reports
------Jacoco
--------jacoco.cov (jacoco execution file, previously used as .exec)
--Module2
--Reports
----Jacoco (Generated HTML, CSV, and XML report files)
----JUnit (JUnit xml report)
At this time, no unit tests exist for Module2. The problem I believe is that the overall coverage on SonarQube reflects only for Module1 and ignores Module2 completely. This makes the coverage appear higher than what it truly is for the whole project. Does anyone have any thoughts on this? Thank you for your input.
If I get your problem correctly you are looking to force coverage to 0% when there are no coverage file generated.
You have to set the correct parameter for this behaviour :
sonar.jacoco.reportMissing.force.zero=true
see documentation for more details : http://docs.sonarqube.org/display/PLUG/Usage+of+JaCoCo+with+Java+Plugin
Related
I have one separate project for main source code and one for test cases. Now while running test project in jenkins job , i want to capture code coverage report for main source code.
I found similar question asked here is:- Java code coverage for other source code repository
Please suggest some answer or else what should be the project structure to generate code coverage report for unit test cases.
The JaCoCo plugin adds a JacocoReport task to your project. You can configure the additionalClassDirs and additionalSourceDirs properties on that task.
Eg:
apply plugin: 'jacoco'
tasks.withType(JacocoReport) {
additionalClassDirs = ...
additionalSourceDirs = ...
}
I have a Gradle project with the JaCoCo plugin applied to it. When I run my tests and create a jacocoTestReport I get this classes not matching error
[ant:jacocoReport] Classes in bundle 'e-services' do no match with execution data. For report generation the same class files must be used as at runtime.
[ant:jacocoReport] Execution data for class eservices/model/persistence/Event does not match.
The classes should match as I'm doing a clean -> build -> test locally. I suspect the mismatch comes from the fact that I'm using jackson.map.ObjectMapper to create an object from a JSON and somehow this causes the classId stored in jacoco's .exec file not match the compiled class id.
My test uses the Event class extensively and still I get 0% coverage due to class mismatch:
import eservices.model.persistence.Event;
event = mapper.readValue(json, Event.class);
event.setTenId(TenIds.getInternalId());
Is there a way to get coverage from this scenario?
This is a JaCoCo Known limitation as JaCoCo relies on the checksum of the runtime bytecodes matching the checksum of the bytecodes it uses for report generation. Typically it happens when you have two libraries instrumenting bytecodes, like PowerMock and JaCoCo, or JPA and JaCoCo.
This is referenced in the following JaCoCo issue Coverage is missing a class that was in fact tested #193 and it is labeled as 'wontfix' 'known limitation'
I have the following command :
java -javaagent:jmockit.jar -cp ./out:junit.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore TestCompareNumbers
where TestCompareNumbers is my test class for which i want a coverage report.
The result is :
JUnit version 4.12-beta-3
.....
Time: 0.011
OK (5 tests)
But a coverage report file hasnt been generated. I guess that Ive missed an option, I searched on google but i have no answer for that.
Thanks!
If your are looking for coverage using jmockit for a non-jmockit based code you should use javaagent= jmockit-coverage.jar
From the jmockit documentation
When not using the JMockit mocking APIs, code coverage can still be activated without adding any jar to the classpath. Instead, run with "-javaagent:/jmockit-coverage.jar" as a JVM initialization parameter.
I do not wand violations to be reported for Java test files in SonarQube, but I do want JUnit and Cobertura code coverage to be analyzed and displayed (JUnit and code coverage reports are reused, not executed by SonarQube).
How do I exclude test files from violations analysis only? I tried adding to global exclusion these settings, but they are not working:
**/test/**
**/Test*.*
Thanks
SonarQube can ignore issues on certain components and against certain coding rules. You might want to read the Ignore Issues on Multiple Criteria section in SonarQube Narrowing the Focus.
As mentioned in the documentation:
You can ignore issues on certain components and for certain coding rules.
Examples:
I want to ignore all issues on all files => *;**/*
I want to ignore all issues on COBOL program bank/ZTR00021.cbl => *;bank/ZTR00021.cbl
I want to ignore all issues on classes located directly in the Java package com.foo, but not in its sub-packages => ;com/foo/
I want to ignore all issues against coding rule cpp.Union on files in the directory object and its sub-directories => cpp.Union;object/**/*
I am using Jenkins with Sonar to perform a build of my Java EE application through an ANT script.
The build works fine and the unit test case run fine as well.
I am currently getting a unit test coverage % but the "Unit test sucess" shows as zero. When I click on the "0 tests" which is available to me as a link on the Sonar dashboard, I get to see the different modules of my Java EE application which Sonar ran through as defined in the build.xml used by Jenkins.
After some research online and reading through a similar Stackoverflow question at this link - Unit test success reported as zero by Sonar
I added the following property tags in my build.xml (used by Jenkins) located in my Java EE application root folder and in the sonar.xml files residing in every sub folder which is defined as a module in the build.xml file:
<property name="sonar.java.coveragePlugin" value="jacoco" />
<property name="sonar.junit.reportsPath" value="ABCD/testreport/" />
<property name="sonar.jacoco.reportPath" value="ABCD/build/classes/jacoco.exec" />
where "ABCD" is the directory which I use to house my Junit test case JAVA files.
The ANT script xml file does contain a junit task which compiles executes my unit test cases in the Jenkins build before giving the hanndle to Sonar. Since I know I am getting a unit test coverage, I know these unit tests are being seen to a degree but what additional steps do I need to take to make the unit test success show up with a value greater than zero?
Please advise.
Thanks in advance.
Subbu