Full name:
- Exclude From Code Coverage
- Jacoco Exclude Package From Coverage Gradle
- Jacoco Exclude From Coverage Annotation
- Jacoco Java
- Jacoco Exclude Package From Coverage Auto Insurance
org.jacoco:jacoco-maven-plugin:0.8.7-SNAPSHOT:check
Description:
Attributes:
If the coverage task is not suitable for your launch target, you might alternatively use the agent task to create the Java agent parameter. The following example defines a Ant property with the name agentvmparam that can be directly used as a Java VM parameter: jacoco:agent property='agentvmparam'/ This task has the same attributes as the coverage task plus an additional. Jacoco is not excluding the files from code coverage. Hi, I have some folder and files which I want to exclude them from code coverage as those files are not accessed when unit test cases are. Jacoco Coverage Gradle Plugin. Jacoco Coverage is a Gradle Plugin that provides two tasks extending the standard Gradle Jacoco plugin: Firstly, the org.wrlyonsjr.jacoco-coverage plugin allows Gradle build scripts to configure minimum Java Code Coverage thresholds for projects, packages, classes, and files.
- Requires a Maven project to be executed.
- Since version:
0.6.1
. - Binds by default to the lifecycle phase:
verify
.
Required Parameters
Name | Type | Since | Description |
| boolean | 0.6.1 | Halt the build if any of the checks fail. Default value is: true .User property is: jacoco.haltOnFailure . |
| List | 0.6.1 | Check configuration used to specify rules on element types(BUNDLE, PACKAGE, CLASS, SOURCEFILE or METHOD) with a list oflimits. Each limit applies to a certain counter (INSTRUCTION, LINE,BRANCH, COMPLEXITY, METHOD, CLASS) and defines a minimum or maximumfor the corresponding value (TOTALCOUNT, COVEREDCOUNT, MISSEDCOUNT,COVEREDRATIO, MISSEDRATIO). If a limit refers to a ratio it must bein the range from 0.0 to 1.0 where the number of decimal placeswill also determine the precision in error messages. A limit ratiomay optionally be declared as a percentage where 0.80 and 80%represent the same value. If not specified the following defaults are assumed:
This example requires an overall instruction coverage of 80% andno class must be missed: This example requires a line coverage minimum of 50% for everyclass except test classes: |
Optional Parameters
Name | Type | Since | Description |
| File | 0.6.1 | File with execution data. Default value is: ${project.build.directory}/jacoco.exec . |
| List | 0.6.1 | A list of class files to exclude from analysis. May use wildcardcharacters (* and ?). When not specified nothing will be excluded. |
| List | 0.6.1 | A list of class files to include into analysis. May use wildcardcharacters (* and ?). When not specified everything will beincluded. |
| boolean | 0.6.1 | Flag used to suppress execution. Default value is: false .User property is: jacoco.skip . |
Parameter Details
- Type:
java.io.File
- Since:
0.6.1
- Required:
No
- Default:
${project.build.directory}/jacoco.exec
- Type:
java.util.List
- Since:
0.6.1
- Required:
No
- Type:
boolean
- Since:
0.6.1
- Required:
Yes
- User Property:
jacoco.haltOnFailure
- Default:
true
- Type:
java.util.List
- Since:
0.6.1
- Required:
No
Check configuration used to specify rules on element types(BUNDLE, PACKAGE, CLASS, SOURCEFILE or METHOD) with a list oflimits. Each limit applies to a certain counter (INSTRUCTION, LINE,BRANCH, COMPLEXITY, METHOD, CLASS) and defines a minimum or maximumfor the corresponding value (TOTALCOUNT, COVEREDCOUNT, MISSEDCOUNT,COVEREDRATIO, MISSEDRATIO). If a limit refers to a ratio it must bein the range from 0.0 to 1.0 where the number of decimal placeswill also determine the precision in error messages. A limit ratiomay optionally be declared as a percentage where 0.80 and 80%represent the same value.
If not specified the following defaults are assumed:
- rule element: BUNDLE
- limit counter: INSTRUCTION
- limit value: COVEREDRATIO
This example requires an overall instruction coverage of 80% andno class must be missed:
This example requires a line coverage minimum of 50% for everyclass except test classes:
- Type:
java.util.List
- Since:
0.6.1
- Required:
Yes
- Type:
boolean
- Since:
0.6.1
- Required:
No
- User Property:
jacoco.skip
- Default:
false
In this article, i am going to explain how to use SonarQube and JaCoCo as a Code Coverage and Source Code Quality analysis tool for Spring Boot application.
What is Code Coverage and why it is important?
Code Coverage is an important topic when it comes to Test Driven Development (TDD). most of the developers are curious to know how percentage of source code is covered with test cases developed (for both unit and integration tests).
Code Coverage shows the stats of how much of source code is covered and tested with test cases (both unit and integration) developed for the application. Therefore the code coverage analysis is an important fact of measuring the quality of the source code. we need to write the test cases to achieve higher code coverage which will increase the maintainability of the source code.
Technology Stack
Optional Parameters
Name | Type | Since | Description |
| File | 0.6.1 | File with execution data. Default value is: ${project.build.directory}/jacoco.exec . |
| List | 0.6.1 | A list of class files to exclude from analysis. May use wildcardcharacters (* and ?). When not specified nothing will be excluded. |
| List | 0.6.1 | A list of class files to include into analysis. May use wildcardcharacters (* and ?). When not specified everything will beincluded. |
| boolean | 0.6.1 | Flag used to suppress execution. Default value is: false .User property is: jacoco.skip . |
Parameter Details
- Type:
java.io.File
- Since:
0.6.1
- Required:
No
- Default:
${project.build.directory}/jacoco.exec
- Type:
java.util.List
- Since:
0.6.1
- Required:
No
- Type:
boolean
- Since:
0.6.1
- Required:
Yes
- User Property:
jacoco.haltOnFailure
- Default:
true
- Type:
java.util.List
- Since:
0.6.1
- Required:
No
Check configuration used to specify rules on element types(BUNDLE, PACKAGE, CLASS, SOURCEFILE or METHOD) with a list oflimits. Each limit applies to a certain counter (INSTRUCTION, LINE,BRANCH, COMPLEXITY, METHOD, CLASS) and defines a minimum or maximumfor the corresponding value (TOTALCOUNT, COVEREDCOUNT, MISSEDCOUNT,COVEREDRATIO, MISSEDRATIO). If a limit refers to a ratio it must bein the range from 0.0 to 1.0 where the number of decimal placeswill also determine the precision in error messages. A limit ratiomay optionally be declared as a percentage where 0.80 and 80%represent the same value.
If not specified the following defaults are assumed:
- rule element: BUNDLE
- limit counter: INSTRUCTION
- limit value: COVEREDRATIO
This example requires an overall instruction coverage of 80% andno class must be missed:
This example requires a line coverage minimum of 50% for everyclass except test classes:
- Type:
java.util.List
- Since:
0.6.1
- Required:
Yes
- Type:
boolean
- Since:
0.6.1
- Required:
No
- User Property:
jacoco.skip
- Default:
false
In this article, i am going to explain how to use SonarQube and JaCoCo as a Code Coverage and Source Code Quality analysis tool for Spring Boot application.
What is Code Coverage and why it is important?
Code Coverage is an important topic when it comes to Test Driven Development (TDD). most of the developers are curious to know how percentage of source code is covered with test cases developed (for both unit and integration tests).
Code Coverage shows the stats of how much of source code is covered and tested with test cases (both unit and integration) developed for the application. Therefore the code coverage analysis is an important fact of measuring the quality of the source code. we need to write the test cases to achieve higher code coverage which will increase the maintainability of the source code.
Technology Stack
The following technologies will be used for this article.
- SonarQube
- Docker
- JaCoCo
- Spring Boot Application with maven
Install and Run SonarQube with Docker
The most of the developers know the 'SonarQube' as a code quality analysis tool. This has the capability of the executing Unit and Integration tests with given library/tool (such as Cobertura, JaCoCo etc..) and it gives a detailed analysis of code coverage of the source code. In this article, we will run SonarQube as a docker image. Therefore we need to have docker installed in our development environment.
If you do not have SonarQube in your local development environment, you can download it with following command.
Once the SonarQube docker image is retrieved, it can be run with following command.
This will start a docker container based on the sonarqube image and give it the name sonarqube. Adding the -d means the container will run in detached mode (background). The -p 9000:9000 and 9092:9092 means that we expose port 9000 and 9092 to the host using the same port numbers.
Now you can navigate to http://localhost:9000 and you will see your local SonarQube dashboard.
JaCoCo Maven configuration
JaCoCo is a one of the famous code coverage library available for java based applications. In oder to add JaCoCo for the project, you have to add the following maven plugin (under the plugins) for the pom.xml of the project.
<plugin> |
<groupId>org.jacocogroupId> |
<artifactId>jacoco-maven-pluginartifactId> |
<version>0.8.0version> |
<executions> |
<execution> |
<id>default-prepare-agentid> |
<goals> |
<goal>prepare-agentgoal> |
goals> |
execution> |
<execution> |
<id>default-reportid> |
<phase>prepare-packagephase> |
<goals> |
<goal>reportgoal> |
goals> |
execution> |
executions> |
plugin> |
(This should be added under the plugins section of the pom.xml of the project)
JaCoCo Test Coverage Analysis with SonarQube
First you need to run the test cases with maven before sending the report for the Sonar server. This can be done with following command.
Exclude From Code Coverage
SonarQube has a really good integration with test code coverage. It allows you to analyze which parts of the code should be better covered, and you can correlate this with many other stats. If you want to send your report to your Sonar server the only thing you need to do is to execute the following command in the terminal. (make sure that you have run the mvn test command successfully before executing the below command)
Then it will send the inspection report to the SonarQube and you can access thedetailed report through http://localhost:9000 using the specified login credentials.
username : admin
password : admin
Run as a Single Command
As you can see that we have used two separate commands for integrating test result analysis with sonar.
Running test cases with maven
Sending the coverage report to sonar
Jacoco Exclude Package From Coverage Gradle
Both of above commands can be composed into one single command as follows.
Jacoco Exclude From Coverage Annotation
Exclude Classes from Code Coverage Analysis
Jacoco Java
In the code coverage analysis we focus only about the classes that should be covered with unit and integration tests. that mens the controllers, repositories, services and domain specific classes. There are some classes which are not covered by either unit or integration tests. In order to get the correct figure of code coverage analysis, it is required to exclude those non related classes when performing code coverage analysis.
E.g:- configuration related classes (SpringBootApplication configuration class, SpringSecurityApplication configuration class etc..) should be avoided
Jacoco Exclude Package From Coverage Auto Insurance
This can be done with adding the classes as classes to be excluded under the 'properties' section of pom.xml.
You can add multiple exclusions and each of them should be separated by comma. According to the above configuration, SpringBootDockerExampleApplication and any class under the config package will be excluded/ignored when performing code coverage analysis.