@TestPropertySource

@TestPropertySource 是一个类级注解,您可以使用它来配置属性文件的位置和内联属性,以添加到为集成测试加载的 ApplicationContextEnvironment 中的 PropertySources 集中。

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

  • Java

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

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

  • Java

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

有关示例和更多详细信息,请参阅 使用测试属性源的上下文配置