当前位置: 首页 > news >正文

Maven工程配置代码覆盖工具Jacoco

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

本篇博文我们将给出示例理解如何在Maven工程中配置Jacoco如何使用Jacoco查看代码覆盖报告~

Jacoco是一个开源的Java代码覆盖率工具,Jacoco可以嵌入到Ant 、Maven中,并提供了EclEmma Eclipse插件,也可以使用JavaAgent技术监控Java程序。很多第三方的工具提供了对Jacoco的集成,如sonar、Jenkins等。

Maven工程

创建Maven工程

打开Eclipse,File->New->Project->Maven Project,新建一个Maven工程~

203134_KFHu_2911530.png

点击“Next”按钮,然后填写groupIdartifactId信息后点击"Finish"按钮即可~

groupId -->  com.xxx.tutorial
artifactId --> jacoco-demo

203625_uqyX_2911530.png

配置Jacoco

添加maven-complier-plugin

           <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.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>

添加jacoco-maven-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>
							<dataFile>target/jacoco.exec</dataFile>
							<outputDirectory>target/jacoco-ut</outputDirectory>
						</configuration>
					</execution>
				</executions>
				<configuration>
					<systemPropertyVariables>
						<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
					</systemPropertyVariables>
				</configuration>
			</plugin>

在这里,我们将单元测试结果的输出目录确定为target/jacoco-ut目录下~

完整的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.xxx.tutorial</groupId>
	<artifactId>jacoco-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<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.7</source>
					<target>1.7</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>
							<dataFile>target/jacoco.exec</dataFile>
							<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>

编写代码

Calculator.java

package com.xxx.tutorial;

/**
 * 
 * @author wangmengjun
 *
 */
public class Calculator {

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

	public int sub(int a, int b) {
		return a - b;
	}
}

Calculator_Test.java

package com.xxx.tutorial;

import org.junit.Assert;
import org.junit.Test;

/**
 * 
 * @author wangmengjun
 *
 */
public class Calculator_Test {

	private Calculator instance = new Calculator();

	@Test
	public void testAdd() {
		int a = 10;
		int b = 20;
		int expected = 30;
		Assert.assertEquals(expected, instance.add(a, b));
	}

	@Test
	public void testSub() {
		int a = 10;
		int b = 20;
		int expected = -10;
		Assert.assertEquals(expected, instance.sub(a, b));
	}
}

代码结构如下:

205937_3uX9_2911530.png

运行并查看Jacoco报告

运行Maven test

210152_1bi1_2911530.png

执行Maven test, 控制台输出如下结果:

210336_h1j0_2911530.png

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building jacoco-demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.5.201505241946:prepare-agent (prepare-agent) @ jacoco-demo ---
[INFO] argLine set to -javaagent:D:\\java_tools\\Reponsitories\\Maven\\org\\jacoco\\org.jacoco.agent\\0.7.5.201505241946\\org.jacoco.agent-0.7.5.201505241946-runtime.jar=destfile=F:\\JavaDeveloper\\workspaces\\SpringMVCDubboExample\\jacoco-demo\\target\\jacoco.exec
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ jacoco-demo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ jacoco-demo ---
[INFO] Not compiling main sources
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ jacoco-demo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.6.1:testCompile (default-testCompile) @ jacoco-demo ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ jacoco-demo ---
[INFO] Surefire report directory: F:\JavaDeveloper\workspaces\SpringMVCDubboExample\jacoco-demo\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.xxx.tutorial.Calculator_Test
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.134 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.5.201505241946:report (post-unit-test) @ jacoco-demo ---
[INFO] Analyzed bundle 'jacoco-demo' with 1 classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.972 s
[INFO] Finished at: 2017-06-16T21:02:30+08:00
[INFO] Final Memory: 15M/244M
[INFO] ------------------------------------------------------------------------

我们可以看到target目录下,已经生成了Jacoco的单元测试结果报告~

210424_jWSv_2911530.png

查看Jacoco报告

打开浏览器,在URL栏输入<工程地址>/target/jacoco-ut/index.html,如:

210651_O9Cq_2911530.png

点击"com.xxx.tutorial"链接,查看这个com.xxx.tutorial包下的类。

211017_G58A_2911530.png

再点击"Calculator"链接,展示Calculator类的方法信息~

211149_flev_2911530.png

再点击任何方法的连接,将会出现该类代码覆盖的情况:

绿色的表示覆盖到的,如果没有覆盖则会用红色背景表示

211344_6ZKH_2911530.png

至此,

在Maven工程中配置Jacoco插件,运行并查看执行报告结果的示例就完成了~

另外,如果Eclipse工程中安装了EclEmma插件,执行测试类,

212049_KyRp_2911530.png

也能得到相应的结果,如:

211629_sOrX_2911530.png

转载于:https://my.oschina.net/wangmengjun/blog/974067

相关文章:

  • linux进程D状态_转
  • MongoDB 聚合查询
  • Android组件 - 收藏集 - 掘金
  • java统计abacbacdadbc中的每个字母出现的次数,输出格式是:a(4)b(3)c(3)d(2)
  • ASP.NET Core 简单实现七牛图片上传(FormData 和 Base64)
  • docker 镜像(四)
  • 笨方法使用Kubernetes实现持续交付
  • You need to use a Theme.AppCompat theme (or descendant) with this activity
  • Android开发学习之事件处理和Button具体解释
  • activity生命周期的onPause和onStop
  • vue 表单提交
  • GitHub GraphQL API已正式可用
  • 《快学Scala》第五章 类
  • 技术人员的发展之路 程序员规划
  • 详谈再论JAVA获取本机IP地址
  • 2018一半小结一波
  • CAP 一致性协议及应用解析
  • chrome扩展demo1-小时钟
  • git 常用命令
  • JS学习笔记——闭包
  • laravel5.5 视图共享数据
  • Next.js之基础概念(二)
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • text-decoration与color属性
  • vue2.0开发聊天程序(四) 完整体验一次Vue开发(下)
  • 当SetTimeout遇到了字符串
  • 二维平面内的碰撞检测【一】
  • 工程优化暨babel升级小记
  • 检测对象或数组
  • 前端每日实战 2018 年 7 月份项目汇总(共 29 个项目)
  • 巧用 TypeScript (一)
  • 全栈开发——Linux
  • 让你成为前端,后端或全栈开发程序员的进阶指南,一门学到老的技术
  • 网络应用优化——时延与带宽
  • 基于django的视频点播网站开发-step3-注册登录功能 ...
  • ​​​​​​​​​​​​​​汽车网络信息安全分析方法论
  • ###51单片机学习(2)-----如何通过C语言运用延时函数设计LED流水灯
  • (003)SlickEdit Unity的补全
  • (02)vite环境变量配置
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (175)FPGA门控时钟技术
  • (2)(2.4) TerraRanger Tower/Tower EVO(360度)
  • (四)Linux Shell编程——输入输出重定向
  • (四)TensorRT | 基于 GPU 端的 Python 推理
  • (太强大了) - Linux 性能监控、测试、优化工具
  • (一)硬件制作--从零开始自制linux掌上电脑(F1C200S) <嵌入式项目>
  • (轉貼) 2008 Altera 亞洲創新大賽 台灣學生成果傲視全球 [照片花絮] (SOC) (News)
  • .NET Core 中的路径问题
  • .NET Reactor简单使用教程
  • .NET 中 GetProcess 相关方法的性能
  • .NET的微型Web框架 Nancy
  • .net解析传过来的xml_DOM4J解析XML文件
  • .net下简单快捷的数值高低位切换
  • .sdf和.msp文件读取
  • @FeignClient注解,fallback和fallbackFactory