zoukankan      html  css  js  c++  java
  • Spring Boot 2.2.x Junit4 升级为Junit5 后的变化、对比 找不到 org.junit.jupiter.api.Test

    遇到的问题:
    使用 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 之前的 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 之后的测试类

    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.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

  • 相关阅读:
    Pandas的merge并对比SQL中join
    常用cron表达式
    基于Python3实现的各类数据库连接和连接池
    JS 原生 push对象到数组中遇到的问题
    H5移动端IOS音频自动播放解决
    微信小程序,购物车模块代码解读
    小程序,wx.request请求数据服务器配置
    &&和||短路逻辑运算
    小程序,wx.request;动态向服务器端请求数据。
    微信小程序页面跳转
  • 原文地址:https://www.cnblogs.com/felixzh/p/12554701.html
Copyright © 2011-2022 走看看