Springboot 多模块项目集成Jacoco统计单元测试覆盖率

本文介绍了如何在Maven项目中配置SurefirePlugin进行本地和远程SonarQube的单元测试覆盖率统计,以及处理target目录下未编译测试代码的问题,同时提到了MavenCompilerPlugin的配置调整。

最外层POM配置

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                    <forkMode>once</forkMode>
                    <reuseForks>true</reuseForks>
                    <useSystemClassLoader>false</useSystemClassLoader>
                    <argLine>@{argLine}</argLine>
                    <includes>
                        <include>**/*Test.java</include>
                        <include>**/*Test*.java</include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.6</version>
                <executions>
                    <execution>
                        <id>my-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <propertyName>surefireArgLine</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>my-report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report-aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

本地统计代码单元测试覆盖率

mvn test jacoco:prepare-agent jacoco:report jacoco:report-aggregate 

远端SonarQube执行

jenkis服务器需要配置Jacoco

  mvn test jacoco:prepare-agent jacoco:report jacoco:report-aggregate sonar:sonar -Dsonar.projectKey=xxxxx -Dsonar.projectName=xxx

注意事项

Q1 target目录下未编译test目录下的代码

常见报错:

Not compiling test sources

Skipping JaCoCo execution due to missing execution data file.

修改插件配置

 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${version.compiler.plugin}</version>
                <configuration>
                     <!--.....-->
                    <skip>false</skip><!--注意此处-->
                    <!--.....-->
                </configuration>
 </plugin>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值