/plushcap/analysis/lambdatest/reporting-code-coverage-using-maven-and-jacoco-plugin

How To Generate Code Coverage Report Using JaCoCo-Maven Plugin

What's this blog post about?

Sure, here's how you can use JaCoCo Maven Plugin for code coverage in a Java project with Maven: 1. Adding Dependency to POM.xml file: Firstly, add the dependency of JaCoCo Maven plugin to your POM.xml file. The POM.xml file is located at the root directory of your project. Here's how you can do it: ```xml <dependencies> <!-- Adding JaCoCo Maven Plugin Dependency --> <dependency> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.6</version> </dependency> </dependencies> ``` 2. Configuring JaCoCo Maven Plugin: Next, configure the JaCoCo Maven plugin in your POM.xml file. You can do this by adding a new <plugin> tag inside the <build> tag of your POM.xml file. Here's how you can do it: ```xml <plugins> <!-- Adding JaCoCo Maven Plugin Configuration --> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.6</version> <executions> <!-- Execution to generate JaCoCo coverage report --> <execution> <id>preparation-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <!-- Execution to run the test cases --> <execution> <id>run-tests</id> <phase>test</phase> <goals> <goal>test</goal> </goals> </execution> <!-- Execution to publish the JaCoCo coverage report --> <execution> <id>report</id> <phase>verify</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> </plugins> ``` 3. Running the Maven Jobs: Now, run the maven jacoco:preparation-agent to prepare the JaCoCo agent for code coverage tracking. You can do this by running the following command in your terminal or command prompt: ```bash mvn jacoco:prepare-agent ``` 4. Running the Test Cases: Next, run the test cases of your project. This will generate a new JaCoCo coverage report. You can do this by running the following command in your terminal or command prompt: ```bash mvn test ``` 5. Generating Code Coverage Report: Finally, run the maven jacoco:report to publish a new coverage report. JaCo offers a simple and easy way to track code coverage score by declaring minimum requirements. Build fails if these requirements are not met else the build is successful. These requirements can be specified as rules in POM.xml. Just specify the new execution specifying ‘check’ goal in POM.xml. Add the below code after the second <execution> tag in POM.xml. <!--Third execution : used to put a check on the entire package--> <execution> <id>jacoco-check</id> <goals> <goal>check</goal> </goals> <configuration> <rules> <rule> <element>PACKAGE</element> <limits> <limit> <counter>LINE</counter> <value>COVEREDRATIO</value> <minimum>0.50</minimum> </limit> </limits> </rule> </rules> </configuration> </execution> 123456789101112131415161718192021222324252627282930313233343536373839404142434454647484950515253545565758596061626364656676869707172737475767778 With this, we are limiting our coverage ratio to 50%. This signifies a minimum of 50% of the code should be covered during the test phase. You can run maven clean verify to check whether the rules set in jacoco:check goal are met or not. The log shows “All coverage checks have been met.” as our code coverage score is 94% which is greater than our minimum 50%. Automation Testing On LambdaTest Selenium Grid using Maven Project with Jacoco Plugin Selenium testing on the cloud helps you attain better browser coverage, increased test coverage, and accelerated time to market. Parallel testing in Selenium helps you achieve the above mentioned requirements. LambdaTest Cloud Selenium Grid is a cloud-based scalable Selenium testing platform that enables you to run your automation scripts on 2000+ different browsers and operating systems. Pre-requisites: To run the test script using JUnit with Selenium, first, we need to set up an environment. You would first need to create an account on LambdaTest. Do make a note of the username and access-key that is available in LambdaTest profile section. We will use this sample project for Java Selenium testing. Importing project to Eclipse IDE: After downloading a zip file of the project: junit-selenium-sample from GitHub, we import it to Eclipse IDE by following the below mentioned steps: Go to your Eclipse IDE, click on the File menu and select Import. A new dialog box appears. Type Maven in the textbox below and select Existing Maven Projects, and then click Next. In the next dialog box, click on Browse and traverse to the project folder. Also, tick the checkbox giving the path to the POM.xml file, and click Finish. Your project will be loaded in Eclipse IDE successfully. Adding dependencies in the POM.xml file: Open the POM.xml, now add the dependencies of JUnit, Selenium, and JaCoCo Maven Plugin. After adding the dependencies to the code of POM.xml should look like this: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"

Company
LambdaTest

Date published
July 23, 2021

Author(s)
Harshit Paul

Word count
5014

Language
English

Hacker News points
None found.


By Matt Makai. 2021-2024.