此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
TestContext 框架支持类
本节描述了支持 Spring TestContext 框架的各种类。
Spring JUnit 4 运行程序
Spring TestContext 框架通过自定义
runner(在 JUnit 4.12 或更高版本上受支持)。通过使用@RunWith(SpringJUnit4ClassRunner.class)
或较短的@RunWith(SpringRunner.class)
变体,开发人员可以实现基于 JUnit 4 的标准单元和集成测试,以及
同时获得 TestContext 框架的好处,例如支持
加载应用程序上下文, 测试实例的依赖注入, 事务测试
method 执行,依此类推。如果你想将 Spring TestContext 框架与
替代运行程序(例如 JUnit 4 的Parameterized
runner) 或第三方 runner
(例如MockitoJUnitRunner
),您可以选择改用 Spring 对 JUnit 规则的支持。
以下代码清单显示了将测试类配置为
使用自定义 Spring 运行Runner
:
-
Java
-
Kotlin
@RunWith(SpringRunner.class)
@TestExecutionListeners({})
public class SimpleTest {
@Test
public void testMethod() {
// test logic...
}
}
@RunWith(SpringRunner::class)
@TestExecutionListeners
class SimpleTest {
@Test
fun testMethod() {
// test logic...
}
}
在前面的示例中,@TestExecutionListeners
配置了空列表,以
禁用默认侦听器,否则需要ApplicationContext
自
通过@ContextConfiguration
.
Spring JUnit 4 规则
这org.springframework.test.context.junit4.rules
package 提供以下 JUnit
4 个规则(在 JUnit 4.12 或更高版本上受支持):
-
SpringClassRule
-
SpringMethodRule
SpringClassRule
是一个 JUnitTestRule
支持 Spring 的类级功能
TestContext Framework 的 Framework,而SpringMethodRule
是一个 JUnitMethodRule
那个辅助
Spring TestContext 框架的实例级和方法级功能。
与SpringRunner
,Spring 基于规则的 JUnit 支持具有以下优点
独立于任何org.junit.runner.Runner
implementation 的 ,因此可以是
与现有的替代运行程序(如 JUnit 4 的Parameterized
) 或
第三方运行程序(例如MockitoJUnitRunner
).
为了支持 TestContext 框架的全部功能,您必须将SpringClassRule
替换为SpringMethodRule
.以下示例显示了正确的方法
要在集成测试中声明这些规则:
-
Java
-
Kotlin
// Optionally specify a non-Spring Runner via @RunWith(...)
@ContextConfiguration
public class IntegrationTest {
@ClassRule
public static final SpringClassRule springClassRule = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Test
public void testMethod() {
// test logic...
}
}
// Optionally specify a non-Spring Runner via @RunWith(...)
@ContextConfiguration
class IntegrationTest {
@Rule
val springMethodRule = SpringMethodRule()
@Test
fun testMethod() {
// test logic...
}
companion object {
@ClassRule
val springClassRule = SpringClassRule()
}
}
JUnit 4 支持类
这org.springframework.test.context.junit4
package 提供以下支持
类(在 JUnit 4.12 或更高版本上受支持):
-
AbstractJUnit4SpringContextTests
-
AbstractTransactionalJUnit4SpringContextTests
AbstractJUnit4SpringContextTests
是一个抽象基测试类,它集成了
具有显式ApplicationContext
testing 支持在
JUnit 4 环境中。当您扩展AbstractJUnit4SpringContextTests
中,您可以访问protected
applicationContext
实例变量,可用于执行显式
bean 查找或测试整个上下文的状态。
AbstractTransactionalJUnit4SpringContextTests
是AbstractJUnit4SpringContextTests
为 JDBC 添加了一些方便的功能
访问。此类需要javax.sql.DataSource
bean 和PlatformTransactionManager
bean 在ApplicationContext
.当您
扩展AbstractTransactionalJUnit4SpringContextTests
中,您可以访问protected
jdbcTemplate
instance 变量,可用于运行 SQL 语句以查询
数据库。您可以使用此类查询来确认之前和之后的数据库状态
运行与数据库相关的应用程序代码,并且 Spring 确保此类查询在
与应用程序代码相同的事务的范围。当与
一个 ORM 工具,一定要避免误报。
如 JDBC 测试支持中所述,AbstractTransactionalJUnit4SpringContextTests
还提供了方便的方法,这些方法
委托给JdbcTestUtils
通过使用上述jdbcTemplate
.
此外AbstractTransactionalJUnit4SpringContextTests
提供了一个executeSqlScript(..)
针对配置的DataSource
.
这些类便于扩展。如果您不需要您的测试类
要绑定到特定于 Spring 的类层次结构,您可以配置自己的自定义测试
类@RunWith(SpringRunner.class) 或 Spring 的 JUnit rules 。 |
用于 JUnit Jupiter 的 SpringExtension
Spring TestContext 框架提供了与 JUnit Jupiter 测试的完全集成
框架,在 JUnit 5 中引入。通过使用@ExtendWith(SpringExtension.class)
中,您可以实现基于 JUnit Jupiter 的标准单元
和集成测试,同时获得 TestContext 框架的好处,
例如支持加载应用程序上下文、测试实例的依赖注入、
事务性测试方法执行,等等。
此外,由于 JUnit Jupiter 中丰富的扩展 API,Spring 提供了 Spring 支持 JUnit 4 和 TestNG 的:
-
测试构造函数、测试方法和测试生命周期回调的依赖项注入 方法。看依赖项注入与
SpringExtension
了解更多详情。 -
对条件的强大支持 基于 SpEL 表达式、环境变量、系统属性的测试执行、 等等。请参阅以下文档
@EnabledIf
和@DisabledIf
在 Spring JUnit Jupiter Testing Annotations 中了解更多详细信息和示例。 -
自定义组合的注释,结合了来自 Spring 和 JUnit Jupiter 的注释。看 这
@TransactionalDevTestConfig
和@TransactionalIntegrationTest
有关更多详细信息,请参阅测试的元注释支持中的示例。
以下代码清单显示了如何配置测试类以使用SpringExtension
与@ContextConfiguration
:
-
Java
-
Kotlin
// Instructs JUnit Jupiter to extend the test with Spring support.
@ExtendWith(SpringExtension.class)
// Instructs Spring to load an ApplicationContext from TestConfig.class
@ContextConfiguration(classes = TestConfig.class)
class SimpleTests {
@Test
void testMethod() {
// test logic...
}
}
// Instructs JUnit Jupiter to extend the test with Spring support.
@ExtendWith(SpringExtension::class)
// Instructs Spring to load an ApplicationContext from TestConfig::class
@ContextConfiguration(classes = [TestConfig::class])
class SimpleTests {
@Test
fun testMethod() {
// test logic...
}
}
由于你也可以使用 JUnit 5 中的注释作为元注释,因此 Spring 提供了@SpringJUnitConfig
和@SpringJUnitWebConfig
编写的注释来简化
测试的配置ApplicationContext
和 JUnit Jupiter。
以下示例使用@SpringJUnitConfig
减少配置量
在前面的例子中使用:
-
Java
-
Kotlin
// Instructs Spring to register the SpringExtension with JUnit
// Jupiter and load an ApplicationContext from TestConfig.class
@SpringJUnitConfig(TestConfig.class)
class SimpleTests {
@Test
void testMethod() {
// test logic...
}
}
// Instructs Spring to register the SpringExtension with JUnit
// Jupiter and load an ApplicationContext from TestConfig.class
@SpringJUnitConfig(TestConfig::class)
class SimpleTests {
@Test
fun testMethod() {
// test logic...
}
}
同样,以下示例使用@SpringJUnitWebConfig
要创建一个WebApplicationContext
用于 JUnit Jupiter:
-
Java
-
Kotlin
// Instructs Spring to register the SpringExtension with JUnit
// Jupiter and load a WebApplicationContext from TestWebConfig.class
@SpringJUnitWebConfig(TestWebConfig.class)
class SimpleWebTests {
@Test
void testMethod() {
// test logic...
}
}
// Instructs Spring to register the SpringExtension with JUnit
// Jupiter and load a WebApplicationContext from TestWebConfig::class
@SpringJUnitWebConfig(TestWebConfig::class)
class SimpleWebTests {
@Test
fun testMethod() {
// test logic...
}
}
请参阅以下文档@SpringJUnitConfig
和@SpringJUnitWebConfig
在 Spring JUnit Jupiter Testing Annotations 中了解更多详细信息。
依赖项注入与SpringExtension
这SpringExtension
实现ParameterResolver
来自 JUnit Jupiter 的扩展 API,它允许 Spring 为测试提供依赖注入
构造函数、测试方法和测试生命周期回调方法。
具体来说,SpringExtension
可以从测试的ApplicationContext
转换为带有
Spring的@BeforeTransaction
和@AfterTransaction
或 JUnit 的@BeforeAll
,@AfterAll
,@BeforeEach
,@AfterEach
,@Test
,@RepeatedTest
,@ParameterizedTest
,
和其他。
构造函数注入
如果 JUnit Jupiter 测试类的构造函数中的特定参数的类型为ApplicationContext
(或其子类型)或带有@Autowired
,@Qualifier
或@Value
,Spring 会注入该特定
参数替换为相应的 bean 或来自测试的ApplicationContext
.
如果 Spring 如果 构造函数被认为是可自动布线的。构造函数被视为 如果满足以下条件之一,则为 autowirable(按优先顺序)。
-
构造函数使用
@Autowired
. -
@TestConstructor
在测试类上存在或元存在,并且autowireMode
属性设置为ALL
. -
默认的测试构造函数 autowire 模式已更改为
ALL
.
看@TestConstructor
有关使用@TestConstructor
以及如何更改 Global Test Constructor autowire 模式。
如果测试类的构造函数被认为是可自动布线的,则 Spring
承担解析构造函数中所有参数的参数的责任。
因此,没有其他ParameterResolver 注册到 JUnit Jupiter 可以解析
参数。 |
测试类的构造函数注入不能与 JUnit 结合使用
木星的 原因是 要使用 |
在以下示例中, Spring 将OrderService
bean 的ApplicationContext
加载自TestConfig.class
到OrderServiceIntegrationTests
构造 函数。
-
Java
-
Kotlin
@SpringJUnitConfig(TestConfig.class)
class OrderServiceIntegrationTests {
private final OrderService orderService;
@Autowired
OrderServiceIntegrationTests(OrderService orderService) {
this.orderService = orderService;
}
// tests that use the injected OrderService
}
@SpringJUnitConfig(TestConfig::class)
class OrderServiceIntegrationTests @Autowired constructor(private val orderService: OrderService){
// tests that use the injected OrderService
}
请注意,此功能允许测试依赖项final
因此是不可变的。
如果spring.test.constructor.autowire.mode
property 是all
(参见@TestConstructor
),我们可以省略@Autowired
在上一个示例中的构造函数上,从而产生以下内容。
-
Java
-
Kotlin
@SpringJUnitConfig(TestConfig.class)
class OrderServiceIntegrationTests {
private final OrderService orderService;
OrderServiceIntegrationTests(OrderService orderService) {
this.orderService = orderService;
}
// tests that use the injected OrderService
}
@SpringJUnitConfig(TestConfig::class)
class OrderServiceIntegrationTests(val orderService:OrderService) {
// tests that use the injected OrderService
}
方法注入
如果 JUnit Jupiter 测试方法或测试生命周期回调方法中的参数为
类型ApplicationContext
(或其子类型)或带有@Autowired
,@Qualifier
或@Value
,Spring 会注入该特定
参数替换为测试的ApplicationContext
.
在以下示例中, Spring 将OrderService
从ApplicationContext
加载自TestConfig.class
到deleteOrder()
测试方法:
-
Java
-
Kotlin
@SpringJUnitConfig(TestConfig.class)
class OrderServiceIntegrationTests {
@Test
void deleteOrder(@Autowired OrderService orderService) {
// use orderService from the test's ApplicationContext
}
}
@SpringJUnitConfig(TestConfig::class)
class OrderServiceIntegrationTests {
@Test
fun deleteOrder(@Autowired orderService: OrderService) {
// use orderService from the test's ApplicationContext
}
}
由于ParameterResolver
支持,您还可以
将多个依赖项注入到单个方法中,不仅来自 Spring,而且来自
从 JUnit Jupiter 本身或其他第三方扩展。
以下示例显示了如何同时让 Spring 和 JUnit Jupiter 注入依赖项
到placeOrderRepeatedly()
测试方法。
-
Java
-
Kotlin
@SpringJUnitConfig(TestConfig.class)
class OrderServiceIntegrationTests {
@RepeatedTest(10)
void placeOrderRepeatedly(RepetitionInfo repetitionInfo,
@Autowired OrderService orderService) {
// use orderService from the test's ApplicationContext
// and repetitionInfo from JUnit Jupiter
}
}
@SpringJUnitConfig(TestConfig::class)
class OrderServiceIntegrationTests {
@RepeatedTest(10)
fun placeOrderRepeatedly(repetitionInfo:RepetitionInfo, @Autowired orderService:OrderService) {
// use orderService from the test's ApplicationContext
// and repetitionInfo from JUnit Jupiter
}
}
请注意,使用@RepeatedTest
来自 JUnit Jupiter 允许测试方法获得访问权限
到RepetitionInfo
.
@Nested
test 类配置
Spring TestContext 框架支持在@Nested
自 Spring Framework 5.0 以来,JUnit Jupiter 中的 test 类;然而,直到Spring
框架 5.3 类级测试配置注释不是从
像来自超类一样封闭类。
Spring Framework 5.3 引入了对继承测试类的一流支持
configuration 的 set,并且此类配置将由
违约。从默认值更改INHERIT
mode 设置为OVERRIDE
模式中,您可以注释
个人@Nested
test 类替换为@NestedTestConfiguration(EnclosingConfiguration.OVERRIDE)
.显式的@NestedTestConfiguration
声明将应用于带注释的测试类以及
它的任何子类和嵌套类。因此,您可以注释顶级测试类
跟@NestedTestConfiguration
,这将应用于其所有嵌套测试类
递 归。
为了允许开发团队将默认值更改为OVERRIDE
–例如
为了与 Spring Framework 5.0 到 5.2 兼容 – 可以更改默认模式
通过 JVM 系统属性或spring.properties
文件位于
classpath 的有关详细信息,请参阅“更改默认封闭配置继承模式”注释。
尽管下面的 “Hello World” 示例非常简单,但它展示了如何声明
通用配置,该配置由@Nested
测试
类。在这个特定示例中,只有TestConfig
Configuration 类为
继承。每个嵌套测试类都提供自己的一组活动配置文件,从而生成一个
不同ApplicationContext
对于每个嵌套的测试类(有关详细信息,请参阅 Context Caching)。请参阅支持的注释列表以查看
哪些注解可以在@Nested
test 类。
-
Java
-
Kotlin
@SpringJUnitConfig(TestConfig.class)
class GreetingServiceTests {
@Nested
@ActiveProfiles("lang_en")
class EnglishGreetings {
@Test
void hello(@Autowired GreetingService service) {
assertThat(service.greetWorld()).isEqualTo("Hello World");
}
}
@Nested
@ActiveProfiles("lang_de")
class GermanGreetings {
@Test
void hello(@Autowired GreetingService service) {
assertThat(service.greetWorld()).isEqualTo("Hallo Welt");
}
}
}
@SpringJUnitConfig(TestConfig::class)
class GreetingServiceTests {
@Nested
@ActiveProfiles("lang_en")
inner class EnglishGreetings {
@Test
fun hello(@Autowired service:GreetingService) {
assertThat(service.greetWorld()).isEqualTo("Hello World")
}
}
@Nested
@ActiveProfiles("lang_de")
inner class GermanGreetings {
@Test
fun hello(@Autowired service:GreetingService) {
assertThat(service.greetWorld()).isEqualTo("Hallo Welt")
}
}
}
TestNG 支持类
这org.springframework.test.context.testng
package 提供以下支持
类:
-
AbstractTestNGSpringContextTests
-
AbstractTransactionalTestNGSpringContextTests
AbstractTestNGSpringContextTests
是一个抽象基测试类,它集成了
具有显式ApplicationContext
testing 支持在
TestNG 环境。当您扩展AbstractTestNGSpringContextTests
中,您可以访问protected
applicationContext
实例变量,可用于执行显式
bean 查找或测试整个上下文的状态。
AbstractTransactionalTestNGSpringContextTests
是AbstractTestNGSpringContextTests
为 JDBC 添加了一些方便的功能
访问。此类需要javax.sql.DataSource
bean 和PlatformTransactionManager
bean 在ApplicationContext
.当您
扩展AbstractTransactionalTestNGSpringContextTests
中,您可以访问protected
jdbcTemplate
instance 变量,可用于运行 SQL 语句以查询
数据库。您可以使用此类查询来确认之前和之后的数据库状态
运行与数据库相关的应用程序代码,并且 Spring 确保此类查询在
与应用程序代码相同的事务的范围。当与
一个 ORM 工具,一定要避免误报。
如 JDBC 测试支持中所述,AbstractTransactionalTestNGSpringContextTests
还提供了方便的方法,这些方法
委托给JdbcTestUtils
通过使用上述jdbcTemplate
.
此外AbstractTransactionalTestNGSpringContextTests
提供了一个executeSqlScript(..)
针对配置的DataSource
.
这些类便于扩展。如果您不需要您的测试类
要绑定到特定于 Spring 的类层次结构,您可以配置自己的自定义测试
类@ContextConfiguration ,@TestExecutionListeners 等 和 by
使用TestContextManager .查看源代码
之AbstractTestNGSpringContextTests ,了解如何检测测试类的示例。 |