I wrote a custom annotation to ignore classes and methods while generating Jacoco coverage report. Please refer to code below:
#Target({ElementType.TYPE,ElementType.METHOD,ElementType.CONSTRUCTOR})
public #interface ExcludeFromJacocoGeneratedReport {
}
This is working fine in my local but when I am running a Jenkins pipeline, classes and methods marked with this custom annotation are not getting ignored in Jacoco coverage report.
Please guide me if I have missed something or some explicit configuration is required before running jenkins pipeline in this case.
Related
After running test and generating coverage report via Jacoco, I realized that there is a static method where a #Data annotation is used and the line of it is marked yellow indicating that it is not tested.
Report
So, is it normal or how can I test that line?
jacoco can ignore code generated by lombok.
put lombok.config in your project’s root
lombok.addLombokGeneratedAnnotation = true
jacoco Release 0.8.0 (2018/01/02)
Methods annotated with #lombok.Generated to better integrate with Lombok >= 1.16.14. Initial analysis and contribution by Rüdiger zu Dohna (GitHub #513).
Methods annotated with #groovy.transform.Generated to better integrate with Groovy >= 2.5.0. Thanks to Andres Almiray for adding the annotation to Groovy (GitHub #610).
jacoco Release 0.8.2 (2018/08/21)
Classes and methods annotated with annotation whose retention policy is runtime or class and whose simple name is Generated are filtered out during generation of report (GitHub #731).
Seems opinion based, but in my opinion there is no point for testing generated code from an external dependency. I would do noting about it and move on.
I am using maven jacoco pugin to generate code coverage report. I am using Powermock for my Junits. I am getting this message at the time of build:
Classes in bundle "XYZ" do not match with execution data. For report generation, same class files must be used at runtime.
[WARNING] Execution data for class "com/abc/example/testEmail" does not match
...
As per my analysis,wherever I have used #PrepareForTest on a particular class, that class is not showing up in the code coverage report.
I am using the following version of Jacoco and Powermock:
Jacoco - 0.7.9
PowerMock - 1.7.1
I have a util class which is final and I have added one private constructor for hide the default public one. How I can get the coverage for this class in sonarqube with jacoco coverage report and build in Jenkins?
public final class Util {
// My contructor
private Util() {
super();
}
}
According to JaCoCo changelog such private empty no-argument constructors are automatically filtered out starting from JaCoCo version 0.8.0. Changelog also notes:
Tools that directly read exec files and embed JaCoCo for this (such as SonarQube or Jenkins) will provide filtering functionality only after they updated to this version of JaCoCo.
Announcement of release of JaCoCo version 0.8.0 states:
Tools that directly read exec files (which is not a final report) and embed JaCoCo for generation of report will provide filtering functionality only after they updated to this version of JaCoCo.
So please follow/wait/etc respective vendors such as
SonarQube - https://jira.sonarsource.com/browse/SONARJAVA-2608
Eclipse EclEmma - https://bugs.eclipse.org/bugs/show_bug.cgi?id=529391
Jenkins - https://github.com/jenkinsci/jacoco-plugin
Reports generated by corresponding version (0.8.0) of integrations developed as part of JaCoCo project by us (Ant Tasks, Maven Plugin and Command Line Interface) provide filtering functionality.
As of today (30 Jan 2018):
update for SonarQube (https://jira.sonarsource.com/browse/SONARJAVA-2608) is supposed to be in not yet released SonarJava plugin version 5.1
update for Jenkins Plugin (https://github.com/jenkinsci/jacoco-plugin/commit/d04b50962a022b615d5085271f1696d9f6080198) is committed but also not yet released
If you configure sonar to use cobertura (and not jacoco) for the code coverage, you could simply exclude that method from code coverage.
That seems easier than writing an artifical test case using reflection.
Sonarqube generates issues as
TestCases should contain tests
regarding that test classes does not have any tests inside but there are methods marked with #Test annotation and classes annotated with #RunWith(SpringJunit4ClassRunner.class).
I have no problem running TestSuites and importing unit test and coverage reports into sonarqube. I checked sonarqube source code and it should find annotations but unfortunately it does not. Rule id for the mentioned issue is S2187.
Is there a configuration for this rule to find annotated methods or is a known issue?
Edit - config:
sonar.projectKey=x
sonar.projectName=x
sonar.projectVersion=1.0
sonar.modules=a,b,c
a.sonar.projectBaseDir=modules/a
b.sonar.projectBaseDir=modules/b
c.sonar.projectBaseDir=modules/c
sonar.sources=src
sonar.tests=test
sonar.java.binaries=../**/classes/production,../**/classes/test
sonar.java.libraries=../../libraries/repository
sonar.junit.reportsPath=junit/
sonar.java.coveragePlugin=jacoco
sonar.jacoco.reporthPath=coverage/jacoco.exec
sonar.language=java
sonar.sourceEncoding=UTF-8
Issue has been fixed by correctly setting sonar.java.test.libraries/binaries attributes as answered in https://stackoverflow.com/a/32197912/343806. They were not not in the runner configuration and after adding raised issues in sonarqube results marked as fixed.
Hi – I wanted to automatically sweep JUnit tests into suites as part of my continuous builds, so I derived a runner from JUnit's Suite which finds all test classes in a package. The runner works just fine, but the results display is less than expected.
I have one class in my testing support package with a #RunWith annotation for my runner. The runner works by reading a property to get the package under test. Set the property and tell JUnit to run the annotated class, and all tests in that package are executed. The name of the suite is reported as the name of the class which has the #RunWith annotation, in both Ant and IntelliJ. My runner has an override for ParentRunner.getName() which returns the name of the package under test. I verified that the string gets into the runner's Description object. What am I missing?
Environment:
JUnit: 4.5
Ant: 1.7.0
IntelliJ IDEA: 8.1
Thanks for whatever direction you can provide.
This is because ANT and IntelliJ use their own runners, so they are building the name based on the test, and not getting the name from your runner. In other words, the runner is delegated to for the purpose of running the test, but not for the purpose of describing it.
I had a similar problem just a few weeks ago and created an open source project because of it.
You may include it via maven
<dependency>
<groupId>com.github.cschoell</groupId>
<artifactId>junit-dynamicsuite</artifactId>
<version>0.2.0</version>
<scope>test</scope>
</dependency>
or download it from the github page, where you will find its documentation as well.
https://github.com/cschoell/Junit-DynamicSuite
There is a Junit runner included which allows to scan either a directory or the classpath for unit tests and filter them by implementing a simple interface.