SpringBoot 不同版本中 JUnit 测试类的写法变化

733 阅读2分钟

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-engineJUnit4中使用的测试引擎。

junit-jupiter-engineJUnit5中使用的测试引擎。

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

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

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

3. 测试环境指定配置文件

SpringBoot 项目中的 test/resources 目录是用于存放测试用的资源文件的。这些文件只在执行测试时使用,不会被打包到最终的应用中。这个目录通常用于存放测试用的配置文件、数据库脚本等。

一般 test/resources 下我都不写专门的 properties 文件,执行单元测试的时候就会直接使用 main/resources 下的 properties 文件,但是如果 test/resources 下有对应的 properties 文件,则会被优先使用。

比如这两个目录下都有一个 application.properties,则单元测试的时候使用的是 test/resources 下的 application.properties,如果该 application.properties 里里面配置的spring.profiles.active=prod,但是 test/resources 目录下没有 application-prod.properties,则会直接使用 main/resources 下的 application-prod.properties,没有则不加载。

另外,在单元测试的时候使用@ActiveProfiles注解可以直接指定配置文件的,比如指定使用 application-test.properties 文件,@ActiveProfiles优先级高于配置文件中的指定spring.profiles.active=prod。测试代码示例:

package com.example.demo.test;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("test") // 指定使用test环境的配置
public class MyTest {

    @Value("${user.name.test:123}")
    private String username;

    // 测试方法
    @Test
    public void test() {
        // 测试代码
        System.out.println("username==============" + username);
    }
}