問題描述:
我們有個多模組專案,由於種種原因(更常見的可能是需要整合測試)測試檔案和原始檔不在一個模組,Jacoco的覆蓋率無法正確顯示,查詢了一些資料,發現中文的例子比較少,就把我自己的Demo貼一下吧。
該demo 專案有三個子模組,其中sub1,sub2包含各自業務邏輯,demo-test作為測試模組。
<modules> <module>sub1</module> <module>sub2</module> <module>demo-test</module> </modules>
首先是兩個分別位於sub1模組及sub2模組的service,以及demo-test模組下的測試程式碼
1.demo/sub1/src/main/java/service/Sub1Service.java
package service; /** * @author tuzhenxian * @date 19-7-22 */ public class Sub1Service { public void doSomething(){ System.out.println("done by sub1"); } }
2.demo/sub2/src/main/java/service/Sub2Service.java
package service; /** * @author tuzhenxian * @date 19-7-22 */ public class Sub2Service { public void doSomething(){ System.out.println("done by sub2"); } }
3.demo/demo-test/src/test/java/IntegrationTest.java
import org.junit.Test; import service.Sub1Service; import service.Sub2Service; /** * @author tuzhenxian * @date 19-7-22 */ public class IntegrationTest { @Test public void testDoSomething(){ Sub1Service sub1Service=new Sub1Service(); Sub2Service sub2Service=new Sub2Service(); sub1Service.doSomething(); sub2Service.doSomething(); } }
接著是主工程的pom檔案,注意宣告瞭goal 為 prepare-agent (這個引數將jacoco的執行時代理作為一個引數傳到test,https://www.eclemma.org/jacoco/trunk/doc/prepare-agent-mojo.html)
<?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> <properties> <jacoco-version>0.8.3</jacoco-version> </properties> <groupId>com.zhenxian</groupId> <artifactId>demo</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>sub1</module> <module>sub2</module> <module>demo-test</module> </modules> <build> <plugins> <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> </executions> </plugin> </plugins> </build> </project>
最後是在demo-test模組下的pom,注意goal為 report-aggregate ,這個goal是jacoco 0.7.7版本以後,專門為多模組覆蓋率顯示所設定,可以統計該模組所依賴的所有其他模組的覆蓋率(如這裡的dependency sub1,sub2)。
<?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"> <parent> <artifactId>demo</artifactId> <groupId>com.zhenxian</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>demo-test</artifactId> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>com.zhenxian</groupId> <artifactId>sub1</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.zhenxian</groupId> <artifactId>sub2</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>${jacoco-version}</version> <executions> <execution> <id>report-aggregate</id> <phase>test</phase> <goals> <goal>report-aggregate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
配置完成,在頂層目錄即/demo 下執行 mvn test,即可在 /demo/demo-test/target/site/jacoco-aggregate/index.html 看到覆蓋率情況。
參考:https://github.com/jacoco/jacoco/wiki/MavenMultiModule
https://prismoskills.appspot.com/lessons/Maven/Chapter_06_-_Jacoco_report_aggregation.jsp