简介
Jacoco 是一个开源的覆盖率工具,针对的语言为java。它可以嵌入到 Ant 、Maven 中,也提供了 EclEmma Eclipse 插件。Jacoco 主要通过代码注入(即 Java Agent)方式来实现覆盖率的功能。很多第三方的工具提供了对 Jacoco 的集成,如:Sonar、Jenkins、IDEA
官方文档:
https://www.eclemma.org/jacoco/trunk/doc/index.html
参考文章:
https://skaygo.blog.csdn.net/article/details/121775806
https://blog.csdn.net/xwj1992930/article/details/98212905
https://blog.csdn.net/weixin_45876795/article/details/111059031
1、引入pom
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>jacoco-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.jacoco/jacoco-maven-plugin -->
<!-- <dependency>-->
<!-- <groupId>org.jacoco</groupId>-->
<!-- <artifactId>jacoco-maven-plugin</artifactId>-->
<!-- <version>0.8.8</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<configuration>
<excludes>
<!-- 这里配置我们想要排除的包路径-->
<exclude>com/tm/test/*</exclude>
<!-- 通过通配符来排除指定格式的类-->
<exclude>**/*Tests.class</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!--定义输出的文件夹-->
<outputDirectory>target/jacoco-report</outputDirectory>
<!--执行数据的文件-->
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<!--要从报告中排除的类文件列表,支持通配符(*和?)。如果未指定则不会排除任何内容-->
<!-- <excludes>**/test/*.class</excludes>-->
<!--包含生成报告的文件列表,支持通配符(*和?)。如果未指定则包含所有内容-->
<!-- <includes></includes>-->
<!--HTML 报告页面中使用的页脚文本。-->
<!-- <footer></footer>-->
<!--生成报告的文件类型,HTML(默认)、XML、CSV-->
<formats>HTML</formats>
<!--生成报告的编码格式,默认UTF-8-->
<outputEncoding>UTF-8</outputEncoding>
<!--抑制执行的标签-->
<!-- <skip></skip>-->
<!--源文件编码-->
<sourceEncoding>UTF-8</sourceEncoding>
<!--HTML报告的标题-->
<title>${project.name}</title>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2、创建类
public class MessageBuilder {
public String getMessage(String name) {
StringBuilder result = new StringBuilder();
if (name == null || name.trim().length() == 0) {
result.append("empty");
} else {
result.append("Hello ").append(name);
}
return result.toString();
}
}
3、编写单元测试类
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MessageBuilderTest {
@Test
public void testGetMessage1() {
MessageBuilder obj = new MessageBuilder();
assertEquals("Hello test", obj.getMessage("test"));
}
@Test
public void testGetMessage2() {
MessageBuilder obj = new MessageBuilder();
assertEquals("empty", obj.getMessage(null));
}
}
4、进行测试并生成覆盖率的统计
mvn clean test