JaCoCo Java代码覆盖率+ Maven示例

Jacoco用于测量应用程序的代码覆盖率。 在本教程中,我们将了解如何在Maven中配置Jacoco,以及如何使用Jacoco查看代码覆盖率报告。

使用的技术:

  • 蚀火星
  • Maven的3.3.9
  • Java 8

1. Eclipse创建Maven Java项目

1.1在Eclipse中创建一个Maven项目File->New->Project->Maven Project ,选择创建一个简单项目,然后单击下一步

1.2输入groupId和artifactId,如以下屏幕所示,然后单击完成。

2.项目结构

该项目具有以下组成部分

  • 示例算术运算类
  • 示例算术运算JUnit测试类
  • 具有Junit和Jacoco依赖项的pom.xml

3. pom.xml

pom.xml所示配置Junit和Jacoco

pom.xml
<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">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mkyong</groupId>
	<artifactId>MathOperations</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Mathematical Operations</name>

	<properties>
		<jacoco.version>0.7.5.201505241946</jacoco.version>
		<junit.version>4.12</junit.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.6.1</version>
				<configuration>
					<skipMain>true</skipMain>
					<skip>true</skip>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.jacoco</groupId>
				<artifactId>jacoco-maven-plugin</artifactId>
				<version>${jacoco.version}</version>
				<executions>
					<execution>
						<id>prepare-agent</id>
						<goals>
							<goal>prepare-agent</goal>
						</goals>
					</execution>
					<execution>
						<id>report</id>
						<phase>prepare-package</phase>
						<goals>
							<goal>report</goal>
						</goals>
					</execution>
					<execution>
						<id>post-unit-test</id>
						<phase>test</phase>
						<goals>
							<goal>report</goal>
						</goals>
						<configuration>
							<!-- Sets the path to the file which contains the execution data. -->

							<dataFile>target/jacoco.exec</dataFile>
							<!-- Sets the output directory for the code coverage report. -->
							<outputDirectory>target/jacoco-ut</outputDirectory>
						</configuration>
					</execution>
				</executions>
				<configuration>
					<systemPropertyVariables>
						<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
					</systemPropertyVariables>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

4.示例算术运算类

在此类中,创建了“ add”方法,该方法接受两个整数参数并返回sum。

ArithmeticOperations.java
package math.operation;

public class ArithmeticOperations {

	public Integer add(Integer a, Integer b)
	{
		return a+b;
	}

}

5.示例算术运算Junit测试类

为“添加”方法创建了测试用例。

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.运行应用程序

右键单击Project-> Run as-> Maven test。 Jacoco输出报告将在jacoco-ut文件夹下的目标目录中生成

7.输出

7.1要查看输出,请转到目标目录并在浏览器的jacoco-ut文件夹中打开index.html。 类ArithmeticOperations总体报告如下所示

7.2单击上图中的每种方法将给出详细的报告。 此处显示绿色线,指示单元测试覆盖了哪条线。

做完了

下载源代码

下载– jacoco-maven-example.zip (2 KB)

参考文献

  1. JaCoCo Java代码覆盖率库
  2. Wikipedia – Java代码覆盖率工具
  3. JaCoCo – Maven插件– EclEmma

翻译自: https://mkyong.com/maven/jacoco-java-code-coverage-maven-example/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值