1、简介
Jacoco是一个开源的覆盖率工具。Jacoco可以嵌入到Ant 、Maven中,并提供了EclEmma Eclipse插件,也可以使用JavaAgent技术监控Java程序。很多第三方的工具提供了对Jacoco的集成,如sonar、Jenkins等。之所以在此引入Jacoco是因为在使用Cobertura的时候,不能完全的适配Jdk 8 Lambda表达式,而Jacoco可以适配。
官网地址:http://www.eclemma.org/jacoco/
2、pom文件中配置Jacoco插件
在pom文件中添加Jacoco插件配置:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</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>
</configuration>
</execution>
</executions>
</plugin>
3、运行Jacoco
在控制台执行命令:
mvn install //运行jacoco,执行单元测试代码
mvn jacoco:report //生成报告
4、查看报告
进入项目/target/site/,其下的整个Jacoco-ut都是报告相关内容,可以点击index.html进行具体查看