Spring Boot 2.2.x Junit4 升级为Junit5 后的变化、对比 找不到 org.junit.jupiter.api.Test

转载之后自用:

作者博客:https://blog.csdn.net/kmesky

原文链接:https://blog.csdn.net/kmesky/article/details/102984592

 

目录

遇到的问题:

原因:

解决方案:

Spring Boot 2.2 之前的测试类

Spring Boot 2.2 之后的测试类

Spring Boot 2.2 之前的 pom.xml

Spring Boot 2.2 之后的 pom.xml

官方文档说明

链接:

部分翻译:


遇到的问题:

使用 maven 创建了一个 parent 项目 A,其 pom.xml 继承 parent 为 spring-boot-starter-parent 2.1.10。

然后创建 module 项目 B,使用 spring initializr 构建项目,用的是 IDEA,当时没有选 Spring Boot 版本,结果默认使用的是 2.2.1。 创建成功之后的pom.xml如下 Spring Boot 2.2 之后的 pom.xml。

修改项目 B 的 pom 的 parent 为 A,结果测试类报错,找不到 org.junit.jupiter.api.Test

原因:

spring boot 2.2 之前使用的是 Junit4 而后续的使用的是Junit5,导致缺少包。

解决方案:

将父工程 A 的 parent 升级为 spring-boot-starter-parent 2.2.1,如果使用了依赖管理 dependencyManagement,需要把里面的 spring-boot-starter-test 版本号改为 与 parent 对应的 2.2.1。

当然,也可以直接指定 module工程B 的 spring-boot-starter-test 版本号改为 与 parent 对应的 2.2.1

Spring Boot 2.2 前后区别

Spring Boot 2.2 之前的测试类

package com.example.demo1;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo1ApplicationTests {
 
    @Test
    public void contextLoads() {
    }
 
}

Spring Boot 2.2 之后的测试类

package com.example.demo;
 
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
 
@SpringBootTest
class DemoApplicationTests {
 
    @Test
    void contextLoads() {
    }
 
}

Spring Boot 2.2 之前的 pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.10.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>   
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Spring Boot 2.2 之后的 pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>

官方文档说明

链接:

https://docs.spring.io/spring-boot/docs/2.2.x/reference/html/spring-boot-features.html#boot-features-testing

部分翻译:

详细见上述链接

25.测试

Spring Boot提供了许多实用程序和注释,可以在测试应用程序时提供帮助。测试支持由两个模块提供:spring-boot-test包含核心项,并spring-boot-test-autoconfigure支持测试的自动配置。大多数开发人员都使用spring-boot-starter-test“入门程序”,该程序同时导入Spring Boot测试模块以及JUnit Jupiter,AssertJ,Hamcrest和许多其他有用的库。

启动程序还带来了老式引擎,因此您可以运行JUnit 4和JUnit 5测试。如果已将测试迁移到JUnit 5,则应排除对JUnit 4的支持,如以下示例所示:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

 

25.3。测试Spring Boot应用程序

Spring Boot应用程序是Spring ApplicationContext,因此除了对普通Spring上下文进行正常测试以外,无需执行任何其他特殊操作即可对其进行测试。

默认情况下,Spring Boot的外部属性,日志记录和其他功能仅在SpringApplication用于创建时才安装在上下文中。

Spring Boot提供了一个@SpringBootTest注释,spring-test @ContextConfiguration当您需要Spring Boot功能时,它可以用作标准注释的替代。注释通过创建ApplicationContext在测试中使用过的来SpringApplication起作用。除了@SpringBootTest提供许多其他注释外,还用于测试应用程序的更多特定部分。

如果使用的是JUnit 4,请不要忘记也将其添加@RunWith(SpringRunner.class)到测试中,否则注释将被忽略。如果您使用的是JUnit 5,则无需添加等价项@ExtendWith(SpringExtension.class)@SpringBootTest并且其他@…Test注释已经在其中进行了注释。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值