How to exclude generated code from a code coverage report? - java

I've got the following jacoco-maven-plugin configuration:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<excludes>
<exclude>**/Header*.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
Which should exclude from a code coverage report all generated java files that begin with Header. Unfortunately, I still see these classes in my code coverage report which makes the coveralls-maven-plugin fail when I call the coveralls:report. The error I get when I call coveralls:report is:
: No source found for HeaderMyClass.java ->
Which makes me think that the JaCoCo coverage report still contains the data for this class which was automatically generated.

Just add exclusions for report goal in jacoco-maven-plugin configuration
<execution>
<id>coverage-report</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*Dto.class</exclude>
<exclude>com/foo/config/*</exclude>
</excludes>
</configuration>
</execution>

Changing the pattern to:
<excludes>
<exclude>**/Header*.*</exclude>
</excludes>
did the trick

Related

SonarQube coverage showing 0% even after exclusion

I been working on a SpringBoot project and I am trying to exclude some of the packages from the SonarQube code coverage.
The package that I want to exclude is com.org.projectname.model. I tried, <sonar.exclusions>**/model/*.java</sonar.exclusions> and <sonar.coverage.exclusions>**/model/*.java</sonar.coverage.exclusions>, sill SonarQube is showing 0% coverage, why is that?. Also I am curious that, do we need to have any specific dependency to use <sonar.exclusions>, as I am not using any additional dependency related to SonarQube?
or is there any other way to achieve this?
<properties>
<sonar.exclusions>
**/model/*.java
</sonar.exclusions>
</properties>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<configuration>
<excludes>
</excludes>
</configuration>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>target/coverage-data/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>target/coverage-data/jacoco-ut.exec</dataFile>
<outputDirectory>target/coverage-reports/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

Jacoco unable to exclude some packages and classes using <exclude> in pom.xml

I am trying to exclude certain package and classes from Jacoco coverage since they are deprecated. I tried adding exclude in Jacoco plugin as below but when I look at the Jenkins report on Jenkins, I see all those classes as well not covered and the number of lines including them.
Here is my dir structure for the things I want to ignore.
com/dir1/dir2/dir3/dir4/dir5/XYZScreeningTask.java
com/dir1/dir2/dir3/dir4/dir6/dir7/XYZScreeningData.java
com/dir1/dir2/dir3/dir4/dir8/xyzscreening/BaseXYZFileProcessor.java
com/dir1/dir2/dir3/dir4/dir8/xyzscreening/XYZScreeningFileProcessor.java
com/dir1/dir2/dir3/dir4/dir8/abccmcactivity/AbcCMSActivityProcessor.java
Here is the pattern added below under plugins
<excludes>
<exclude>com/dir1/dir2/dir3/dir4/**/*xyz*/**</exclude>
<exclude>com/dir1/dir2/dir3/dir4/**/*XYZ*.*</exclude>
<exclude>com/dir1/dir2/dir3/dir4/**/*abc*/**</exclude>
<exclude>com/dir1/dir2/dir3/dir4/**/*Abc*.*</exclude>
</excludes>
And here is the plugin info where exclusions has been added.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<excludes>
<exclude>com/dir1/dir2/dir3/dir4/**/*xyz*/**</exclude>
<exclude>com/dir1/dir2/dir3/dir4/**/*XYZ*.*</exclude>
<exclude>com/dir1/dir2/dir3/dir4/**/*abc*/**</exclude>
<exclude>com/dir1/dir2/dir3/dir4/**/*Abc*.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>instrument</id>
<phase>process-classes</phase>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>restore</id>
<phase>prepare-package</phase>
<goals>
<goal>restore-instrumented-classes</goal>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>pre-unit-test</id>
<phase>none</phase>
</execution>
<execution>
<id>post-unit-test</id>
<phase>none</phase>
</execution>
<execution>
<id>pre-integration-test</id>
<phase>none</phase>
</execution>
<execution>
<id>post-integration-test</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
As the Jacoco documentation for excludes says
A list of class files to exclude from the report. May use wildcard characters (* and ?). When not specified nothing will be excluded.
You will need to specify classpath of the compiled classes. I fail to see a pattern in the files and packages that you want to exclude so if you want to exclude specific classes.
<excludes>
<exclude>com/dir1/dir2/dir3/dir4/dir6/dir7/XYZScreeningData.class</exclude>
<exclude>com/dir1/dir2/dir3/dir4/dir8/xyzscreening/BaseXYZFileProcessor.class</exclude>
<exclude>com/dir1/dir2/dir3/dir4/dir8/xyzscreening/XYZScreeningFileProcessor.class</exclude>
<exclude>com/dir1/dir2/dir3/dir4/dir8/abccmcactivity/AbcCMSActivityProcessor.class</exclude>
</excludes>
Nevertheless, We can exclude the complete package by something like follow,
<exclude>com/dir1/**/*</exclude>
The more about wildcards can be found From here and here. Which says.
* matches zero or more characters
** matches zero or more directories
? matches a single character

Code Coverage for Cucumber Tests using Jacoco

I am trying to integrate Jacoco to get the code coverage for my Cucumber tests using Maven. Following is my project structure:
-src-main-java-Pages
-src-main-java-Helper
-src-test-java-resources-features
-src-test-java-Steps
Following is the Jacoco configuration in my POM.xml
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco.exec</destFile>
<dataFile>${basedir}/target/coverage-reports/jacoco.exec</dataFile>
<outputDirectory>${basedir}/target/coverage-reports/jacoco</outputDirectory>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<includes>
<include>**/*Steps.*</include>
</includes>
</rule>
<rule>
<element>PACKAGE</element>
<excludes>
<exclude>**/*Pages.*</exclude>
<exclude>**/*Page.*</exclude>
</excludes>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
I am able to generate the code coverage report. However, in the report, it covers all the classes in the -src-main packages. As per different google researches and SO posts, I tried to modify my POM to exclude Pages and include only Steps in my code coverage report. But somehow, it keeps on displaying all the files in the main package.
Am I taking the wrong approach? I just want to create a report which does code coverage for only the tests that I execute rather than all the files in the main package.
See https://maven.apache.org/guides/mini/guide-configuring-plugins.html - you specify exclusions in configuration of
<execution>
<id>check</id>
<goals>
<goal>check</goal>
but not in configuration of
<execution>
<id>report</id>
<goals>
<goal>report</goal>
so it has effect on check, but not on report.

My Jacoco report is showing very little code coverage

I am trying to get an idea of how much code coverage I have with some unit tests in Scala and with the source code in Java(tests and code are in different packages). When I run the Jacoco report, it is showing only 4 percent code coverage.
When I go take a look at the report, it shows 0 percent for a lot of files that I have created tests for. What I suspect is that the unit tests are not being included in the report, maybe since all of the test class files are in the target/test-classes directory.
I have tried including the includeTests tag but that didn't have any effect.
Here is how the plugin looks like in the pom:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<configuration>
<includeTests>true</includeTests>
<!--<includes>
<include>**/test-classes/**</include>
</includes>-->
<excludes>
<exclude>**/dto/**</exclude>
<exclude>**/exceptions/**</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Right now I have commentted out the include tag becasue if I include it, the test compiles with 0 classes. How else can I can include the target/test-classes directory?
Sorry if this is a simple fix, I'm very new with unit testing and Jacoco. Thanks!
This is my personal setup. It's a bit messy and I'm still working out some parts of it but it works for me. You could try this.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>0.20</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>

Instruct Jacoco and Coveralls to ignore some files from the report

I use Coveralls in a Java project with Maven to create a coverage report for my project and I want to avoid some classes from being included in the coverage computation.
Now, I think that I the build phase I can exlcude the class, e.g. MyClassTest.java as follows:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>**/*MyClassTest.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>4.1.0</version>
</plugin>
Anyway, the coverage doesn't decrease, so I guess that the class MyClass is still computed from the Jacoco report.
In the travis file I call the report phase as follows:
- mvn clean test jacoco:report coveralls:report
Any idea?
My bad!
First the .java has to be removed and replace with .class as correctly suggested by Tunaki.
Second, I don't have to exclude the test class, but the class that I don't want to measure in the coverage!
So it is:
<excludes>
<exclude>**/*MyClass</exclude>
</excludes>
instead of
<excludes>
<exclude>**/*MyClassTest.java</exclude>
</excludes>

Categories