具有动态属性的@TestPropertySource [英] @TestPropertySource with dynamic properties
问题描述
我正在使用@TestPropertySource覆盖我的Spring Boot应用程序集成测试中的application.yml属性.
I am using @TestPropertySource to overwrite application.yml properties in my integration test for a spring boot app.
@TestPropertySource(properties = { "repository.file.path=src/test/resources/x" })
我想知道是否有某种方法可以使属性VALUE动态化.像这样:
I was wondering if there was some way to make the property VALUE dynamic. Something like this:
@TestPropertySource(properties = { "repository.file.path=PropertyValueProvider.class" })
感谢您的反馈.就我而言,该属性值是系统特定的,应在测试运行时生成.
Your feedback is appreciated. In my case the property value is system specific that should be generated upon the test run.
推荐答案
@TestPropertySource
仅提供用于配置PropertySource
的声明性机制. 文档在Spring参考手册中.
@TestPropertySource
only provides declarative mechanisms for configuring PropertySource
s. Documentation in Spring Reference Manual.
如果需要程序支持将PropertySource
添加到Environment
,则应实现一个ApplicationContextInitializer
,可以通过@ContextConfiguration(initializers = ...)
注册. 文档在Spring参考手册中.
If you need programmatic support for adding a PropertySource
to the Environment
, you should implement an ApplicationContextInitializer
which can be registered via @ContextConfiguration(initializers = ...)
. Documentation in Spring Reference Manual.
此致
Sam (Spring TestContext Framework的作者)
这篇关于具有动态属性的@TestPropertySource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!