我正在使用@TestPropertySource覆盖应用程序。yml属性在我的Sping Boot 应用程序的集成测试中。
@TestPropertySource(properties = { "repository.file.path=src/test/resources/x" })
我想知道是否有某种方法可以使属性VALUE动态化。就像这样:
@TestPropertySource(properties = { "repository.file.path=PropertyValueProvider.class" })
感谢您的反馈。在我的例子中,属性值是系统特定的,应该在测试运行时生成。
3条答案
按热度按时间cclgggtu1#
@TestPropertySource
仅提供用于配置PropertySource
的 * 声明性 * 机制。Spring参考手册中的文档。如果您需要将
PropertySource
添加到Environment
的编程支持,您应该实现一个可以通过@ContextConfiguration(initializers = ...)
注册的ApplicationContextInitializer
。Spring参考手册中的文档。致上,
Sam (Spring TestContext Framework的作者)
af7jpaap2#
你也可以在Sping Boot 5中使用
@DynamicPropertySource
annotation。2.这使您能够以编程方式(动态地)设置属性值。参见:https://github.com/spring-projects/spring-framework/issues/24540
将以下内容添加到您的集成测试类:
lsmepo6l3#
感谢Sam Brannen's answer
下面是示例代码: