@RunWith 和 @SpringBootTest 注解的使用方法

@RunWith

RunWith注解 When a class is annotated with @RunWith or extends a class
annotated with @RunWith, JUnit will invoke the class it references to
run the tests in that class instead of the runner built into JUnit. We
added this feature late in development. While it seems powerful we
expect the runner API to change as we learn how people really use it.
Some of the classes that are currently internal will likely be refined
and become public.

当一个类用@RunWith注释或继承一个用@RunWith注释的类时,JUnit将调用它所引用的类来运行该类中的测试而不是开发者去在junit内部去构建它。我们在开发过程中使用这个特性。


@SpringBootTest

配置文件属性的读取

可以在运行Spring引导测试的测试类上指定的注释。在常规Spring TestContext框架之上和之上提供以下特性:

当定义没有特定的@ContextConfiguration(loader=…)时,使用SpringBootContextLoader作为默认的ContextLoader。

当不使用嵌套@Configuration时,自动搜索@SpringBootConfiguration,并且没有指定显式的类。

允许使用properties属性定义自定义环境属性。

为不同的webEnvironment模式提供支持,包括启动一个完全运行的web服务器,监听一个已定义的或随机的端口。

注册一个TestRestTemplate和/或WebTestClient bean,用于在web测试中使用完全运行的web服务器。


1、springboot test
https://www.jianshu.com/p/72b19e24a602

2、https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html

<think>嗯,用户想了解Spring Boot中@RunWith@SpringBootTestJUnit的@Test注解之间的区别。首先,我需要分别理解这三个注解的功能用途,然后比较它们的异同。 首先,@Test是JUnit的核心注解,用于标记测试方法。不管是JUnit4还是JUnit5,都需要用@Test来标识测试方法。不过在JUnit5中,虽然还是用@Test,但包路径变了,从org.junit.Test变成了org.junit.jupiter.api.Test。用户可能知道这点,但需要明确区分不同版本的使用。 接下来是@RunWith,这是JUnit4的注解,用于指定测试运行器。在Spring Boot测试中,通常使用SpringRunner.class,这样测试类就能启动Spring上下文。但到了JUnit5,这个注解被@ExtendWith替代,所以用户需要注意他们使用的是哪个版本的JUnit。比如在JUnit5中,用@ExtendWith(SpringExtension.class)来替代原来的@RunWith,而JUnit4则需要保留@RunWith。 然后是@SpringBootTest,这个注解用于创建整个Spring应用的上下文,适合集成测试。它会加载完整的应用配置,并启动嵌入式服务器等。无论JUnit4还是JUnit5都需要这个注解来启用Spring Boot测试支持。不过,在JUnit5中,可以不需要搭配@RunWith或@ExtendWith,因为@SpringBootTest已经自动处理了这些。不过有些情况下可能需要显式使用@ExtendWith,但根据引用内容,JUnit5中可能可以省略。 现在需要比较这三个注解的作用域搭配使用情况。比如,@Test是方法级的,而@RunWith@SpringBootTest是类级的。在JUnit4中,@SpringBootTest需要配合@RunWith使用,而在JUnit5中不需要。用户可能混淆了这些版本差异,需要明确指出。 用户的问题还涉及到这些注解的区别,所以需要分点说明每个注解的功能、适用场景以及版本差异。同时,引用的资料中提到了JUnit4JUnit5的不同处理方式,需要将这些信息整合到回答中,确保准确性。 最后,生成相关问题时要围绕这三个注解的可能疑问,比如版本迁移、常见错误、如何选择测试策略等。这样可以帮助用户进一步理解相关知识点。</think>### 逐步解答:@RunWith@SpringBootTest与@Test注解的区别 #### 1. **@Test(JUnit 注解)** - **功能**:标记一个方法为测试方法,由测试框架执行。 - **用途**:所有单元测试集成测试的核心入口,仅作用于方法级别。 - **版本差异**: - JUnit 4:`import org.junit.Test;` - JUnit 5:`import org.junit.jupiter.api.Test;`(包路径变化) - **示例**: ```java @Test public void testAddition() { Assert.assertEquals(4, 2 + 2); } ``` #### 2. **@RunWith(JUnit 4 注解)** - **功能**:指定测试运行器(Test Runner),用于扩展测试框架行为[^3]。 - **用途**: - 在 JUnit 4 中,与 Spring 集成时需使用`@RunWith(SpringRunner.class)`,以启动 Spring 上下文[^1]。 - 其他场景:如搭配`MockitoJUnitRunner`进行 Mock 测试。 - **版本差异**: - JUnit 4:必须显式使用。 - JUnit 5:被`@ExtendWith`替代,例如`@ExtendWith(SpringExtension.class)`[^4]。 #### 3. **@SpringBootTest(Spring Boot 注解)** - **功能**:创建完整的 Spring 应用上下文,支持集成测试环境[^2]。 - **用途**: - 加载完整的应用配置(如数据库连接、外部服务)。 - 启动嵌入式 Web 服务器(可选)。 - **搭配规则**: - JUnit 4:必须与`@RunWith(SpringRunner.class)`一起使用。 - JUnit 5:可单独使用,无需`@ExtendWith`(Spring 自动适配)[^4]。 #### 对比总结 | 注解 | 作用层级 | 适用版本 | 核心功能 | |-------------------|------------|----------|--------------------------------------| | `@Test` | 方法 | JUnit 4/5| 定义测试方法 | | `@RunWith` | 类 | JUnit 4 | 指定测试运行器(如集成 Spring) | | `@SpringBootTest` | 类 | JUnit 4/5| 加载 Spring 上下文,支持集成测试环境 | #### 常见组合示例 - **JUnit 4**: ```java @RunWith(SpringRunner.class) @SpringBootTest public class IntegrationTest { @Test public void testService() { /* ... */ } } ``` - **JUnit 5**: ```java @SpringBootTest public class IntegrationTest { @Test public void testService() { /* ... */ } } ``` --- ###
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值