前言
對于大多數 maven 多模塊化工程,可以使用 Jacoco 這款工具,關于 Jacoco 這款工具,ChatGPT 對它的描述是這樣的:
JaCoCo(Java Code Coverage)是一個開源的測驗覆寫率工具,它可以用于幫助開發人員衡量其軟體測驗的有效性,它支持多種語言,包括 Java 和 Kotlin 等,并且可以與多個構建工具和集成開發環境(IDE)一起使用,
JaCoCo 可以收集測驗覆寫率資料,并生成可視化的測驗覆寫率報告,幫助開發人員更好地理解其代碼的測驗覆寫率情況,它提供了多種測驗覆寫率指標,例如行覆寫率、分支覆寫率、方法覆寫率、類覆寫率等,可以幫助開發人員了解其測驗覆寫率情況的具體細節,
JaCoCo 還可以與多種構建工具集成,例如 Maven、Gradle 等,它可以通過 Maven 或 Gradle 的插件來收集測驗覆寫率資料,并在構建程序中生成測驗覆寫率報告
Jacoco 可以很好的支持對 Maven 多模塊進行聚合分析測驗覆寫率,可以從專案整體輸出覆寫率報告非常方便,
下面展示一下具體的使用方法
一:創建根專案
先創建一個多模塊的 Maven 專案,大致的結構如下:
├── parent-project
├── pom.xml
├── business-module1
│ ├── pom.xml
│ └── src
│ ├── main
│ └── test
├── business-module2
│ ├── pom.xml
│ └── src
│ ├── main
│ └── test
└── test-module
├── pom.xml
└── src
├── main
└── test
在一個空白的目錄,一個的 Maven 的根專案:
mvn archetype:generate \
-DgroupId=org.example \
-DartifactId=jacoco-multi-module-example \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
然后進入目錄:
cd jacoco-multi-module-example
把根目錄 pom.xml 的 packaging 屬性改為 pom,從而將根目錄設定為一個聚合模塊,用來管理多個子模塊的依賴關系
<packaging>pom</packaging>
二:創建子模塊
根據上面的結構,在根目錄下,分別創建:
- business-module1
- business-module2
- test-module
在根目錄的路徑下,輸入以下命令,創建 business-module1 模塊:
mvn archetype:generate \
-DgroupId=org.example \
-DartifactId=business-module1 \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
創建 business-module2 模塊:
mvn archetype:generate \
-DgroupId=org.example \
-DartifactId=business-module2 \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
創建 test-module 單元測驗模塊:
mvn archetype:generate \
-DgroupId=org.example \
-DartifactId=test-module \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
然后模擬實際的開發,分別在模塊1,模塊2中添加一些業務代碼,
在 business-module1 中我添加一個簡單的數學運算 IntegerSimpleCompute 類:
// business-module1\src\main\java\org\example\IntegerSimpleCompute.java
package org.example;
public class IntegerSimpleCompute {
public int add(int i, int j) {
return i + j;
}
public int subtract(int i, int j) {
return i - j;
}
public int multiply(int i, int j) {
return i * j;
}
public int divide(int i, int j) {
return i / j;
}
}
在 business-module2 中我添加一個簡單的邏輯運算 IntegerLogicCompute 類:
// business-module2\src\main\java\org\example\IntegerLogicCompute.java
package org.example;
public class IntegerLogicCompute {
public int increment(Integer i) {
return i + 1;
}
public int decrement(Integer i) {
return i- 1;
}
// 存在條件分支的陳述句,需要滿足所有條件分支判斷才能達到 100% 的覆寫率
public boolean equals(Integer i, Integer j) {
if (i < 127 && j < 127) {
return i == j;
}
return i.equals(j);
}
}
三:創建測驗模塊
我們將 test-module 作為測驗模塊,在該模塊的 pom.xml 檔案中,我們引入上面的測驗模塊,方便對他們進行集成測驗
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>business-module1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>business-module2</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
然后在 src/test/java 目錄下創建測驗類:
// test-module\src\test\java\org\example\IntegrationTest.java
package org.example;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class IntegrationTest {
private IntegerSimpleCompute simpleCompute;
private IntegerLogicCompute logicCompute;
@Before
public void init() {
simpleCompute = new IntegerSimpleCompute();
logicCompute = new IntegerLogicCompute();
}
@Test
public void simpleComputeTest() throws Throwable {
assertEquals(7, simpleCompute.add(3, 4));
assertEquals(4, simpleCompute.subtract(7, 3));
assertEquals(12, simpleCompute.multiply(3, 4));
assertEquals(3, simpleCompute.divide(12, 4));
}
@Test
public void logicComputeTest() throws Throwable {
assertEquals(8, logicCompute.increment(7));
assertEquals(6, logicCompute.decrement(7));
assertEquals(true, logicCompute.equals(125, 125));
assertEquals(false, logicCompute.equals(123, 125));
assertEquals(false, logicCompute.equals(123, 130));
assertEquals(false, logicCompute.equals(133, 125));
assertEquals(true, logicCompute.equals(140, 140));
assertEquals(false, logicCompute.equals(140, 141));
}
}
到可以,你可以通過:
mvn test
執行單元測驗,maven 的 maven-surefire-plugin 插件也會簡單的輸出如下測驗報告:
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
四:生成覆寫率報告
首先在根目錄的 pom.xml 引入 jacoco 插件并且啟動代理:
<build>
<plugins>
<!-- 指定 Java 編譯版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<!-- jacoco 插件 -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<!-- 執行 prepare-agent 目標,它會啟動 JaCoCo 代理 -->
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- 執行 mvn verify 時,生成測驗覆寫率報告 -->
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
然后在 test-module 模塊中引入 jacoco 插件,宣告一個聚合分析任務:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<!-- 在執行 mvn verify 時,生成聚合測驗覆寫率報告,所有 Maven 子模塊的測驗覆寫率資料 -->
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
最后在根目錄執行指令,運行所有測驗:
$ mvn clean verify
構建成功后可以在 test-module 模塊下的 target/site/jacoco-aggregate/index.html 查看覆寫率報告:

點擊對應模塊可以看到包內部所有類,方法還有每一行的測驗覆寫率情況,這里具體不再展開,自己可以嘗試以下
示例代碼:jacoco-module-sample
參考資料:
- JaCoCo Java Code Coverage Library
- jacoco-multi-module-sample
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/546319.html
標籤:其他
上一篇:Spring簡介-IOC
