Spring Boot 2.2.x Junit4 升級爲Junit5 後的變化、對比 找不到 org.junit.jupiter.api.Test

目錄

遇到的問題:

原因:

解決方案:

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註釋已經在其中進行了註釋。
發佈了10 篇原創文章 · 獲贊 8 · 訪問量 6105
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章