work hard work smart

专注于Java后端开发。 不断总结,举一反三。
随笔 - 1158, 文章 - 0, 评论 - 153, 阅读 - 186万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

jacoco多模块生成java单元测试报告实践

Posted on   work hard work smart  阅读(774)  评论(0编辑  收藏  举报

1、根目录的pom.xml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.7</version>
    <executions>
        <execution>
            <id>prepare-agent</id>
            <goals>
               <goal>prepare-agent</goal>
            </goals>
        </execution>
    </executions>
</plugin>

  

模块关系

----parent

|---- test-module 单元测试模块

| ---- business-module1 业务模块1

| ---- business-module2 业务模块2

test-module 依赖 business-module1 和  business-module2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!--把依赖的模块解压出来放到classes目录下,原依赖的模块都是jar包方式存在,mvn test时会找不到-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <!--把每个依赖的模块,都配置进来-->
                    <artifactItem>
                        <groupId>com.lishiots.cloud.datacenter</groupId>
                        <artifactId>datacenter-service</artifactId>
                        <version>1.0.1</version>
                        <type>jar</type>
                        <includes>**/*.class</includes>
                        <overWrite>false</overWrite>
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <!--  junit 5 版本至少为2.22.2,且需要增加其他依赖 -->
    <version>2.7.1</version>
    <configuration>
        <runOrder>random</runOrder>
        <testFailureIgnore>true</testFailureIgnore>
    </configuration>
</plugin>
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.7</version>
    <executions>
        <!--整合所有子模块报告-->
        <execution>
            <id>report-aggregate</id>
            <phase>test</phase>
            <goals>
                <goal>report-aggregate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

  

所有的单元测试代码再test-module的src/test/java 下

test-module的pom.xml

 

依次执行如下命令

1
2
3
4
5
6
7
8
9
10
先编译
 
mvn clean -U -Dmaven.test.skip=true package
 
保证test-module的target/classes中存在依赖的模块代码
如果启动需要依赖yml文件,需要先替换target/classes/bootstart.yml中的占位符
 
再执行测试
 
mvn test

  

参考:https://www.jianshu.com/p/4c2af38bc85f

https://blog.csdn.net/skh2015java/article/details/121775806

 

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2021-06-19 Python unittest和HTMLTestRunner生成报告
2021-06-19 Python 单元测试
2021-06-19 Python requests库使用
2015-06-19 Andaroid L新特性
点击右上角即可分享
微信分享提示