jacoco 测试代码覆盖率 趟坑记录

项目springboot版本

spring-boot:2.0.4-RELEASE

1.公司要求项目使用jaCoCo查看测试代码覆盖率,于是添加依赖

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.1</version>
    <configuration>
        <skipTests>false</skipTests>
        <excludes>
            <exclude>**/IT*.java</exclude>
        </excludes>
    </configuration>
</plugin>
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.4</version>
    <executions>
        <execution>
            <id>jacoco-initialize</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>post-unit-test</id>
            <phase>package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

执行命令行:

mvn test

结果没有跑到Junit5的测试用例

查看一下,发现在maven3.6以上版本才有对junit5的支持,早期版本需要添加对应的依赖才能支持

springboot使用junit5/junit4_尚云峰的博客-CSDN博客_springboot集成junit5

修改maven-surefire-plugin和添加maven-failsafe-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.1</version>
    <configuration>
        <skipTests>false</skipTests>
        <!--suppress UnresolvedMavenProperty -->
        <argLine>${surefireArgLine}</argLine>
        <!-- Excludes integration tests when unit tests are run. -->
        <excludes>
            <exclude>**/IT*.java</exclude>
        </excludes>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.1.0</version>
        </dependency>
    </dependencies>
</plugin>
<!--
<plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>3.0.0-M5/version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>
-->

再次执行命令,还是失败

Some problems were encountered while building the effective model for com.xforceplus:coop-service:jar:1.0.0-SNAPSHOT 'parent.relativePath' of POM com.xforceplus:coop-service:1.0.0-SNAPSHOT (E:\MyWork\WorkSpace\IDEASpace-ANT-Bitbucket\ant-coop-service\coop-service\pom.xml) points at com.xforceplus:ant-coop-service instead of com.xforceplus:xplat-parent, please verify your project structure @ line 15, column 13 It is highly recommended to fix these problems because they threaten the stability of your build. For this reason, future Maven versions might no longer support building such malformed projects.

原因,项目中有parent,需要添加

<relativePath/>

之后:

<parent>
    <groupId>com.xxx</groupId>
    <artifactId>xxx</artifactId>
    <version>xx</version>
    <relativePath/>
</parent>

执行命令后正常,但是还是没有找到junit5的用例,重新找了一下,发现网上surefire版本跟自己的不一样,修改成2.19.1版本,重新尝试:

Results : Tests run: 328, Failures: 0, Errors: 0, Skipped: 0

成功。

记录一下完整依赖:

<parent>
    <groupId>com.xxx</groupId>
    <artifactId>xxx</artifactId>
    <version>xx</version>
    <relativePath/>
</parent>

<dependencies>
......
</dependencies>

<plugins>
......
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <skipTests>false</skipTests>
        <!--suppress UnresolvedMavenProperty -->
        <argLine>${surefireArgLine}</argLine>
        <!-- Excludes integration tests when unit tests are run. -->
        <excludes>
            <exclude>**/IT*.java</exclude>
        </excludes>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.1.0</version>
        </dependency>
    </dependencies>
</plugin>
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.4</version>
    <executions>
        <execution>
            <id>jacoco-initialize</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>
        <execution>
            <id>post-unit-test</id>
            <phase>package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>
......
</plugins>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值