博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Maven工程配置代码覆盖工具Jacoco
阅读量:6188 次
发布时间:2019-06-21

本文共 6177 字,大约阅读时间需要 20 分钟。

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

org.apache.maven.plugins
maven-compiler-plugin
3.6.1
true
true
1.7
1.7

添加jacoco-maven-plugin

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

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

完整的pom.xml

4.0.0
com.xxx.tutorial
jacoco-demo
0.0.1-SNAPSHOT
0.7.5.201505241946
4.12
junit
junit
${junit.version}
test
org.apache.maven.plugins
maven-compiler-plugin
3.6.1
true
true
1.7
1.7
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

编写代码

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_TestTests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.134 secResults :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

你可能感兴趣的文章
Flex结合java实现一个登录功能
查看>>
12篇学通C#网络编程——第一篇 基础之进程线程(转)
查看>>
我的WCF之旅(1):创建一个简单的WCF程序
查看>>
JUnit学习摘要+入门实例
查看>>
[转]MS Sql 7105错误
查看>>
架构之路--实战项目记录(一) 概述
查看>>
追赶法解方程
查看>>
使用批处理build vs2005的工程
查看>>
Git 命令行(cygwin) + Git Extensions + Git Source Control Provider
查看>>
PHP可以通过什么组件上传大文件
查看>>
c#(asp.net/core)杂谈笔记
查看>>
Is there a complete List of JVM exit codes
查看>>
星空-许巍
查看>>
关于iOS和OS X废弃的API知识点
查看>>
【安全组网】思科IOS设备基础应用
查看>>
Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor...
查看>>
转: 在CentOS 6.X 上面安装 Python 2.7.X
查看>>
转:WaitForSingleObject()函数、WaitForMultipleObject()函数
查看>>
SQLServer-----SQLServer 2008 R2安装
查看>>
bzoj 4873: [Shoi2017]寿司餐厅 [最小割]
查看>>