springboot 不同版本中 junit 测试类的写法变化

本文详细介绍了SpringBoot从2.2之前到2.2之后,以及2.4.5版本中JUnit测试类的写法变化。在2.2之前,测试类使用@RunWith(SpringRunner.class)和@SpringBootTest注解;从2.2开始,引入了JUnit5,需要排除junit-vintage-engine以避免冲突;而在2.4.5版本中,由于已移除junit-vintage-engine,可以直接使用spring-boot-starter-test依赖。

springboot 不同版本中 junit 测试类的写法变化

参考网址:

https://blog.csdn.net/wb1046329430/article/details/116450264?ops_request_misc=&request_id=&biz_id=102&utm_term=老版springboot测试类&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-0-.ecpm_v1_rank_v29&spm=1018.2226.3001.4187

说明:

该作者整理的比较全了,防止遗忘,转载一波

1. SpringBoot 2.2 之前的版本

1.1. 测试类

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 SampTest {
 
    @Test
    public void test() {
    }
 
}

1.2. 依赖项

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

2. SpringBoot 2.2 之后的版本

2.1. 测试类

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

2.2. 依懒项

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
	<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>
</dependencies>

junit-vintage-engine是JUnit4中使用的测试引擎。
junit-jupiter-engine是JUnit5中使用的测试引擎。

在Spring Boot 2.2之后版本的spring-boot-starter-test中提供了junit-vintage-engine和junit-jupiter,而junit-jupiter中又提供了junit-jupiter-engine。也就是同时集成了JUnit4和JUnit5中的测试引擎,这样是为了兼容老版本考虑。如果你的项目只用了JUnit5,防止出错,可以在pom中进行排除junit-vintage-engine。

如下所示为Spring Boot 2.3.5版本的spring-boot-starter-test的pom依赖:

<dependency>
	<groupId>org.junit.jupiter</groupId>
	<artifactId>junit-jupiter</artifactId>
	<version>5.6.3</version>
	<scope>compile</scope>
</dependency>
<dependency>
	<groupId>org.junit.vintage</groupId>
	<artifactId>junit-vintage-engine</artifactId>
	<version>5.6.3</version>
	<scope>compile</scope>
	<exclusions>
		<exclusion>
			<artifactId>hamcrest-core</artifactId>
			<groupId>org.hamcrest</groupId>
		</exclusion>
	</exclusions>
</dependency>

这是junit-jupiter中的pom依赖:

<dependency>
	<groupId>org.junit.jupiter</groupId>
	<artifactId>junit-jupiter-engine</artifactId>
	<version>5.6.3</version>
	<scope>runtime</scope>
</dependency>

2.3. 最新依赖

目前最新版的Spring Boot 2.4.5是不需要进行排除的,因为我发现该版本的spring-boot-starter-test中已经将junit-vintage-engine依赖删除了。所以pom可以直接这么写了(和2.2之前的版本一样了,用到了可以自己去查看你所用SpringBoot版本的spring-boot-starter-test,看是否需要排除Junit4依赖):

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.5</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>

.springframework.boot
spring-boot-starter-test
test


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值