SpringBootTest-初始化上下文之前执行方法

应用案例:在加载 SpringBoot 配置前,想启动 H2 TCP 数据库,使 SpringBoot 配置文件中用到的数据库连接地址生效。

### SpringBoot2.2.x-Junit5版:

方案1:使用 @BeforeAll 注解方式。

import org.h2.tools.Server;
import org.junit.jupiter.api.BeforeAll;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class BaseTest {

    @BeforeAll
    public static void before(){
        try {
            Server.main("-tcp", "-tcpAllowOthers", "-webAdminPassword", "admin");
        } catch (Exception throwables) {
            throwables.printStackTrace();
            System.err.println("数据库启动失败!");
            System.exit(-1);
        }
    }

}

注:@BeforeAll 注解修饰的方法必须是 static 的静态方法,在 SpringBoot 初始化之前执行。@BeforeEach 注解修饰的方法必须是 非 静态方法,在 SpringBoot 初始化之后,测试用例执行前 才执行。

方案2:使用构造方法方式。

import org.h2.tools.Server;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class BaseTest {

    public BaseTest(){
        try {
            Server.main("-tcp", "-tcpAllowOthers", "-webAdminPassword", "admin");
        } catch (Exception throwables) {
            throwables.printStackTrace();
            System.err.println("数据库启动失败!");
            System.exit(-1);
        }
    }

}

### SpringBoot2.1.x-Junit4版:

方案1:使用 @BeforeClass 注解方式。

import org.h2.tools.Server;
import org.junit.BeforeClass;
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 BaseTest {

    @BeforeClass
    public static void before(){
        try {
            Server.main("-tcp", "-tcpAllowOthers", "-webAdminPassword", "admin");
        } catch (Exception throwables) {
            throwables.printStackTrace();
            System.err.println("数据库启动失败!");
            System.exit(-1);
        }
    }

}

注:Junit5 @BeforeAll 注解 等同于 Junit4 @BeforeClass 注解,必须使用在 static 的静态方法上。

方案2:使用构造方法方式(同上)。

 

`@RunWith(SpringRunner.class)` 和 `@SpringBootTest` 都是用于简化单元测试与集成测试在 Spring 应用上下文中的编写过程的 JUnit 注解。它们分别在测试类级别和方法级别上发挥作用。 #### @RunWith(SpringRunner.class) `@RunWith(SpringRunner.class)` 注解是一个运行器(runner),它负责在单元测试环境中初始化 Spring 上下文,通常包含配置文件(如 application.properties 或 application.yml 文件)、依赖注入、以及所有必需的 bean 初始化。这使得开发者能够在一个独立于实际部署环境的测试环境下运行单元测试。 ### 示例 ```java @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class MyApplicationTests { // 测试类体... } ``` 在这个例子中,当 `MyApplicationTests` 类被运行时,SpringRunner 会自动启动 Spring 应用上下文,并配置好所有需要的服务、数据源、bean 等。这意味着你可以直接调用服务层或数据访问层的 API 来验证其功能,而无需考虑实际生产环境中的复杂细节。 #### @SpringBootTest 相比之下,`@SpringBootTest` 注解则是在整个测试类级别的作用范围,它同样负责创建 Spring 上下文并加载默认的 Spring 配置。但是,它的灵活性更高,允许开发者指定多种 Web 环境,如无界面测试(`webEnvironment = SpringBootTest.WebEnvironment.NONE`)、随机端口测试(`webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT`)或者其他自定义的配置。 ### 示例 ```java @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class MyControllerTest { // 测试方法... } ``` 在这个例子中,每个测试方法都将针对一个随机开放的 HTTP 端口进行测试,假设有一个 Web 应用作为背景。 ### 使用场景差异 - **@RunWith(SpringRunner.class)** 更多地应用于对整个测试框架的控制,特别是当测试需要特定的 Runner 来处理底层的测试执行流程时; - **@SpringBootTest** 则更多关注于上下文的创建和配置,默认配置通常满足大部分测试需求,同时提供了一些额外的配置选项来适应各种测试环境的要求。 通过这两个注解的组合使用,开发者可以有效地组织和执行针对 Spring 应用的各种测试场景,保证应用程序的功能性和稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值