java maven jacoco_JaCoCo Java Code Coverage + Maven example

Jacoco is used to measure the code coverage of application. In this tutorial we will understand how to configure Jacoco in maven and how to use Jacoco to see code coverage report.

Technologies Used:

Eclipse Mars

Maven 3.3.9

Java 8

1. Eclipse Create Maven Java Project

1.1 In Eclipse create a Maven project File->New->Project->Maven Project, Select create a simple project and click on next

dac1aa9fe7f4ae71e9eba93f651a6d10.png

1.2 Enter groupId & artifactId as shown in below screen and click on finish.

9b143d83d3aecc640c8900b3258a30f1.png

2. Project Structure

The project has the following components

Sample Arithmetic Operations Class

Sample Arithmetic Operations JUnit Test Class

pom.xml with Junit and Jacoco dependencies

a58da8af8f914a164a584dda28398f1a.png

3. pom.xml

Configure Junit and Jacoco as shown in pom.xml

pom.xml

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">

4.0.0

com.mkyong

MathOperations

0.0.1-SNAPSHOT

Mathematical Operations

0.7.5.201505241946

4.12

junit

junit

${junit.version}

test

org.apache.maven.plugins

maven-compiler-plugin

3.6.1

true

true

1.8

1.8

org.jacoco

jacoco-maven-plugin

${jacoco.version}

prepare-agent

prepare-agent

report

prepare-package

report

post-unit-test

test

report

target/jacoco.exec

target/jacoco-ut

target/jacoco.exec

4. Sample Arithmetic Operations Class

In this class method ‘add’ is created which accepts two integer parameters and returns sum.

ArithmeticOperations.java

package math.operation;

public class ArithmeticOperations {

public Integer add(Integer a, Integer b)

return a+b;

5. Sample Arithmetic Operations Junit Test Class

Testcase is created for ‘add’ method.

ArithmeticOperationsTest.java

package math.operation;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class ArithmeticOperationsTest {

@Test

public void testAdd()

{

ArithmeticOperations operations = new ArithmeticOperations();

Integer actual = operations.add(2, 6);

Integer expected = 8;

assertEquals(expected, actual);

6. Run Application

Right Click Project->Run as ->Maven test. Jacoco output report will be generated in target directory under jacoco-ut folder

8bc665356408bcf2d054f5c2db436da9.png

7. Output

7.1 To see the output go to target directory and open index.html from jacoco-ut folder in browser. Overall Report for class ArithmeticOperations is shown below

a6e513c2bfd173d1186d6a69e75bbe90.png

7.2 Clicking on each method in above figure gives detailed report. Here it shows green coloured line indicating which line is covered by unit test.

31c18b50a60d6af0359c8614c7aaba9d.png

Done.

References

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值