org.junit.jupiter.api.Test和org.junit.Test

今天单元测试遇到一个问题,在引入org.junit.Test做单元测试时,IDEA左边的启动标志Run Test不显示。很郁闷...

最终解决是删除了导入的org.junit.Test重新导入org.junit.jupiter.api.Test后启动标志才出现的。

查阅了下资料,原理是:

    spring boot 2.2之前使用的是 Junit4

    spring boot 2.2之后使用的是 Junit5


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() {

    }

}

此外呢,还有pom文件也有区别:

    Spring Boot 2.2 之后的 pom.xml

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

    Spring Boot 2.2 之后的 pom.xml

<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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容