|
对于最新的稳定版本,请使用 Spring Framework 6.2.10! |
使用测试属性源的上下文配置
Spring Framework 对具有属性源层次结构的环境概念提供了一流的支持属性源,您可以使用特定于测试的属性源配置集成测试。与@PropertySource用于@Configuration类,您可以声明@TestPropertySource测试上的注释类来声明测试属性文件或内联属性的资源位置。这些测试属性源将添加到PropertySources在Environment对于ApplicationContext加载以进行带注释的集成测试。
|
您可以使用 实现 |
声明测试属性源
您可以使用locations或value属性@TestPropertySource.
默认情况下,传统和基于 XMLjava.util.Properties文件格式为支持——例如,"classpath:/com/example/test.properties"或"file:///path/to/file.xml". 从 Spring Framework 6.1 开始,您可以配置自定义PropertySourceFactory通过factory属性@TestPropertySource为了支持不同的文件格式,例如 JSON、YAML 等。
每条路径都被解释为一个弹簧Resource. 普通路径(例如"test.properties")被视为相对于包定义测试类的类路径资源。以斜杠开头的路径被视为absolute 类路径资源(例如:"/org/example/test.xml"). 路径引用 URL(例如,以classpath:,file:或http:) 是使用指定的资源协议加载。
路径中的属性占位符(例如${…}) 将针对Environment.
从 Spring Framework 6.1 开始,还支持资源位置模式—— 例"classpath*:/config/*.properties".
以下示例使用测试属性文件:
-
Java
-
Kotlin
@ContextConfiguration
@TestPropertySource("/test.properties") (1)
class MyIntegrationTests {
// class body...
}
| 1 | 指定具有绝对路径的属性文件。 |
@ContextConfiguration
@TestPropertySource("/test.properties") (1)
class MyIntegrationTests {
// class body...
}
| 1 | 指定具有绝对路径的属性文件。 |
您可以使用properties属性@TestPropertySource,如下一个示例所示。 都 键值对添加到封闭Environment作为单次测试PropertySource具有最高优先级。
键值对支持的语法与为Java 属性文件中的条目定义的语法相同:
-
key=value -
key:value -
key value
|
尽管可以使用上述任何语法变体和任意数量的键和值之间的空格来定义属性,但建议您使用一种语法变体和测试套件中的一致间距 — 例如,考虑始终 用 原因是您提供的确切字符串将用于确定 上下文缓存。因此,要从上下文缓存中受益,您必须确保 您可以一致地定义内联属性。 |
以下示例设置两个内联属性:
-
Java
-
Kotlin
@ContextConfiguration
@TestPropertySource(properties = {"timezone = GMT", "port = 4242"}) (1)
class MyIntegrationTests {
// class body...
}
| 1 | 通过字符串数组设置两个属性。 |
@ContextConfiguration
@TestPropertySource(properties = ["timezone = GMT", "port = 4242"]) (1)
class MyIntegrationTests {
// class body...
}
| 1 | 通过字符串数组设置两个属性。 |
从 Spring Framework 6.1 开始,您可以使用文本块来定义多个内联
单个属性String.以下示例使用
文本块:
-
Java
-
Kotlin
@ContextConfiguration
@TestPropertySource(properties = """
timezone = GMT
port = 4242
""") (1)
class MyIntegrationTests {
// class body...
}
| 1 | 通过文本块设置两个属性。 |
@ContextConfiguration
@TestPropertySource(properties = ["""
timezone = GMT
port = 4242
"""]) (1)
class MyIntegrationTests {
// class body...
}
| 1 | 通过文本块设置两个属性。 |
|
从 Spring Framework 5.2 开始, 此外,您可以在测试类上声明多个组合注释,每个注释都是
meta-annotated 直接存在 |
默认属性文件检测
如果@TestPropertySource被声明为空注解(即,没有显式
值locations或properties属性),则尝试检测
默认属性文件。例如
如果带注释的测试类是com.example.MyTest,相应的默认属性
file 是classpath:com/example/MyTest.properties.如果无法检测到默认值,则IllegalStateException被抛出。
优先
测试属性的优先级高于作系统的
环境、Java 系统属性或应用程序添加的属性源
声明性地使用@PropertySource或以编程方式。因此,测试属性可以
用于有选择地覆盖从系统和应用程序属性加载的属性
来源。此外,内联属性的优先级高于加载的属性
从资源位置。但请注意,通过@DynamicPropertySource有
比通过@TestPropertySource.
在下一个示例中,timezone和port属性和"/test.properties"覆盖在系统中定义的任何同名属性
和应用程序属性源。此外,如果"/test.properties"文件定义
条目timezone和port被内联
使用properties属性。以下示例演示了如何
要在文件和内联中指定属性:
-
Java
-
Kotlin
@ContextConfiguration
@TestPropertySource(
locations = "/test.properties",
properties = {"timezone = GMT", "port = 4242"}
)
class MyIntegrationTests {
// class body...
}
@ContextConfiguration
@TestPropertySource("/test.properties",
properties = ["timezone = GMT", "port = 4242"]
)
class MyIntegrationTests {
// class body...
}
继承和覆盖测试属性源
@TestPropertySource支持布尔值inheritLocations和inheritProperties属性,表示属性文件和内联的资源位置
应继承超类声明的属性。两个标志的默认值
是true.这意味着测试类继承位置和内联属性
由任何超类声明。具体来说,位置和内联属性
test 类附加到超类声明的位置和内联属性。
因此,子类可以选择扩展位置和内联属性。注意
稍后出现的属性 shadow (即覆盖) 同名属性
出现得更早。此外,上述优先规则适用于继承的
测试属性源。
如果inheritLocations或inheritProperties属性@TestPropertySource是
设置为false,分别是测试类的位置或内联属性
shadow 并有效地替换超类定义的配置。
从 Spring Framework 5.3 开始,测试配置也可以从封闭
类。看@Nested测试类配置了解详情。 |
在下一个示例中,ApplicationContext为BaseTest仅使用base.properties文件作为测试属性源。相比之下,ApplicationContext为ExtendedTest通过使用base.properties和extended.properties文件作为测试属性源位置。以下示例演示如何定义
使用properties文件:
-
Java
-
Kotlin
@TestPropertySource("base.properties")
@ContextConfiguration
class BaseTest {
// ...
}
@TestPropertySource("extended.properties")
@ContextConfiguration
class ExtendedTest extends BaseTest {
// ...
}
@TestPropertySource("base.properties")
@ContextConfiguration
open class BaseTest {
// ...
}
@TestPropertySource("extended.properties")
@ContextConfiguration
class ExtendedTest : BaseTest() {
// ...
}
在下一个示例中,ApplicationContext为BaseTest仅使用
内联key1财产。相比之下,ApplicationContext为ExtendedTest是
使用内联key1和key2性能。以下示例演示了如何
要使用内联属性定义子类及其超类中的属性:
-
Java
-
Kotlin
@TestPropertySource(properties = "key1 = value1")
@ContextConfiguration
class BaseTest {
// ...
}
@TestPropertySource(properties = "key2 = value2")
@ContextConfiguration
class ExtendedTest extends BaseTest {
// ...
}
@TestPropertySource(properties = ["key1 = value1"])
@ContextConfiguration
open class BaseTest {
// ...
}
@TestPropertySource(properties = ["key2 = value2"])
@ContextConfiguration
class ExtendedTest : BaseTest() {
// ...
}