对于最新稳定版本,请使用Spring Framework 7.0.1spring-doc.cadn.net.cn

带测试属性源的上下文配置

Spring 框架对具有属性源层级结构的环境概念提供了一流的支持,你可以配置集成测试中的测试。属性源。与@PropertySource注释用于@Configuration你可以声明@TestPropertySource测试上的注释用于声明测试属性文件或内联属性的资源位置。这些测试属性源被添加到地产来源环境对于应用上下文加载用于注释积分测试。spring-doc.cadn.net.cn

你可以使用@TestPropertySource对于任何实现SmartContextLoader虽然是SPI但@TestPropertySource不支持旧版本的实现上下文加载器SPI。spring-doc.cadn.net.cn

实现SmartContextLoader通过getPropertySourceLocations()getPropertySourceProperties()方法MergedContextConfiguration.spring-doc.cadn.net.cn

声明测试属性来源

你可以通过使用地点属性@TestPropertySource.spring-doc.cadn.net.cn

支持传统和基于XML的属性文件格式——例如,“classpath:/com/example/test.properties”“file:///path/to/file.xml”.spring-doc.cadn.net.cn

每条路径都被视为一个泉水资源. 一条平路径(例如,“test.properties”)被视为相对于定义测试类的包的类路径资源。该包定义了测试类。以斜杠开头的路径被视为绝对类路径资源(例如:“/org/example/test.xml”). 一条路径引用一个URL(例如,前缀为Classpath:,文件:http:) 是通过指定的资源协议加载。资源位置通配符(例如**/*。性能不允许:每个位置必须计算到恰好一个。性能.xml资源。spring-doc.cadn.net.cn

以下示例使用了一个测试属性文件:spring-doc.cadn.net.cn

@ContextConfiguration
@TestPropertySource("/test.properties") (1)
class MyIntegrationTests {
	// class body...
}
1 指定一个带有绝对路径的属性文件。
@ContextConfiguration
@TestPropertySource("/test.properties") (1)
class MyIntegrationTests {
	// class body...
}
1 指定一个带有绝对路径的属性文件。

你可以通过使用性能属性@TestPropertySource,如下一个示例所示。都 键值对被添加到包围中环境作为单一测试地产来源最高优先级。spring-doc.cadn.net.cn

键值对的支持语法与 中条目的语法相同 一个 Java 属性文件:spring-doc.cadn.net.cn

以下示例设定了两个内嵌性质:spring-doc.cadn.net.cn

@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,@TestPropertySource可以作为可重复的注释使用。 这意味着你可以有多个声明@TestPropertySource在单曲上 测试类,其中地点性能以后@TestPropertySource覆盖前述注释@TestPropertySource附注。spring-doc.cadn.net.cn

此外,你可以在测试类上声明多个组合注释,每个注释分别是 元注释为@TestPropertySource,以及所有这些@TestPropertySource声明将有助于你的测试性质来源。spring-doc.cadn.net.cn

直接出现@TestPropertySource注释始终优先于注解 元现在@TestPropertySource附注。换句话说,地点性能直接现在@TestPropertySource注释将覆盖地点性能来自@TestPropertySource注释用作 元注释。spring-doc.cadn.net.cn

默认属性文件检测

如果@TestPropertySource被声明为空注释(即没有显式 以下数值地点性能属性),尝试检测 默认属性文件相对于声明该注释的类。例如 如果注释测试类为com.example.MyTest,相应的默认属性 文件是classpath:com/example/MyTest.properties.如果无法检测到默认值,则非法州例外被抛出。spring-doc.cadn.net.cn

优先

测试属性的优先级高于作系统定义的 环境、Java 系统属性,或应用程序添加的属性源 声明式通过以下方式进行@PropertySource或者程序化。因此,测试性质 用于选择性地覆盖系统和应用属性加载的属性 来源。此外,内嵌属性的优先级高于负载属性 来自资源位置。但请注意,财产登记方式如下@DynamicPropertySource有 优先级高于通过@TestPropertySource.spring-doc.cadn.net.cn

在下一个例子中,该时区端口性质以及定义在“/test.properties”覆盖系统中定义的同名属性 以及应用属性来源。此外,如果“/test.properties”文件定义 关于时区端口这些属性被内行覆盖 通过使用性能属性。以下示例展示了如何 要在文件和内联中同时指定属性:spring-doc.cadn.net.cn

@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支持布尔值继承地点继承属性属性表示属性文件的资源位置是否内联 由超类声明的属性应继承。两个标志的默认值 是true.这意味着测试类继承了位置和内联属性 由任何超级宣布。具体来说,是 测试类附加在超类声明的位置和内嵌属性上。 因此,子类可以选择扩展位置和内嵌属性。注意 这些后来出现的属性会遮蔽(即覆盖)同名的属性 更早出现的。此外,上述优先规则也适用于继承 也要查查属性来源。spring-doc.cadn.net.cn

如果继承地点继承属性属性@TestPropertySource是 设置为false,分别是测试类的位置或内联属性 遮蔽并有效地替换了由超类定义的配置。spring-doc.cadn.net.cn

从 Spring Framework 5.3 起,测试配置也可以继承自随附的版本 类。看@Nested测试类配置细节。

在下一个例子中,该应用上下文基础测试仅通过使用base.properties(基础属性)作为测试属性源提交文件。相比之下,应用上下文扩展测试通过使用base.properties(基础属性)扩展属性文件作为测试属性源位置。以下示例展示了如何定义 通过使用性能文件:spring-doc.cadn.net.cn

@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() {
	// ...
}

在下一个例子中,该应用上下文基础测试仅通过使用 内联密钥1财产。相比之下,应用上下文扩展测试是 通过使用内联加载密钥1密钥2性能。以下示例展示了如何 通过使用内联性质定义子类及其超类的性质:spring-doc.cadn.net.cn

@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() {
	// ...
}