跳到主要内容

@TestPropertySource

ChatGPT-4o-mini 中英对照 @TestPropertySource @TestPropertySource

@TestPropertySource 是一个可以应用于测试类的注解,用于配置属性文件的位置和内联属性,以便将其添加到为集成测试加载的 ApplicationContextEnvironment 中的 PropertySources 集合中。

以下示例演示如何从类路径声明一个属性文件:

@ContextConfiguration
@TestPropertySource("/test.properties") 1
class MyIntegrationTests {
// 类体...
}
java
  • 从类路径根目录中的 test.properties 获取属性。

以下示例演示如何声明内联属性:

@ContextConfiguration
@TestPropertySource(properties = { "timezone = GMT", "port: 4242" }) 1
class MyIntegrationTests {
// class body...
}
java
  • 声明 timezoneport 属性。

请参阅 上下文配置与测试属性源 以获取示例和更多详细信息。