springboot升级后mvn test找不到单元测试

Springboot版本从2.1.11.RELEASE升级到2.4.3之后发现执行 mvn test找不到单元测试执行了。

之前了解过某个 2.x 版本后的spring-boot-test默认使用Junit5作为默认测试执行器,而现有代码中的测试使用的是Junit4进行编写,怀疑是此原因造成。

junit5相关文档中找到了相关描述,意思是说,想要 maven-surefire-plugin执行 Junit 的测试,在依赖中必须要声明一个TestEngine的实现:

<!-- Junit5 配置实现 -->
<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
    </plugins>
</build>
<!-- ... -->
<dependencies>
    <!-- ... -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.7.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.7.1</version>
        <scope>test</scope>
    </dependency>
    <!-- ... -->
</dependencies>

同时也支持 Junit4 的测试用例,需要增加 Junit4 和 Junit Vintage依赖,可以同时执行 Junit4 和 Junit5 的测试用例

<!-- ... -->
<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
    </plugins>
</build>
<!-- ... -->
<dependencies>
    <!-- ... -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>5.7.1</version>
        <scope>test</scope>
    </dependency>
    <!-- ... -->
</dependencies>
<!-- ... -->

增加依赖后,再执行mvn test测试用例的确可以正常运行了。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
demo参考网上资料,使用mvn建项,使用者需要有一定mvn基础。 demo没有实现复杂业务,只实现了部分功能: 微服务模块初始化时,常量和数据库信息等使用云配置服务(spring config)获取; 微服务之间使用负载均衡(ribbon); 微服务网关路由配置; 微服务断路器(hystrix)及监听服务等 启动步骤: 1.启动server-eureka,端口6600,微服务注册中心 访问http://localhost:6600,查看效果 2.启动server-config,端口6700,统一配置服务中心 访问http://localhost:6700/service-order/online,查看效果 3.启动service-order,端口6002,初始化使用配置服务server-config动态加载数据库 访问http://localhost:6002/order,查看效果 4.启动service-user,端口6001,使用注册后的服务名service-order进行服务之间调用,避免传统维护困难的ip:port方式 模块中使用了ribbon负载均衡请求service-order,需要启动至少两个service-order服务 访问http://localhost:6001/user/order,查看效果 5.启动gateway-zuul,端口6000,用于url路由配置,服务统一端口入口 http://localhost:6000/service-order/order等效于访问http://localhost:6002/order http://localhost:6000/service-user/user/order等效于访问http://localhost:6001/user/order 6.启动hystrix-dashboard,端口6500,可选,WEB界面查看监听服务,如服务成功多少,失败多少等信息 进入hystrix-dashboard界面后,填入监控地址:http://localhost:6001/hystrix.stream

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值