0
点赞
收藏
分享

微信扫一扫

SpringBoot单元测试用法

蛇发女妖 2022-02-25 阅读 138

参考链接:

现象:
使用下面版本的springboot,单元测试类下的@RunWith爆红

<!-- 父model -->
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/>
</parent>
<!-- 子model -->
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
</dependency>

正确用法:
Spring Boot 2.2 之前的测试类

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 之后的测试类

<!-- 子model -->
<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>

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
 
@SpringBootTest(classes = DemoApplication.class)
class DemoApplicationTests {
 
    @Test
    void contextLoads() {
    }
 
}

总结:存在以上的现象,是由于Spring Boot 2.2.x Junit4 升级为Junit5 后的变化。

举报

相关推荐

0 条评论