我想知道 @TestPropertySource
将遵守 SpEL 或至少允许一个属性替换另一个属性的值。
这是与 @TestPropertySource with dynamic properties 类似的问题
假设我所指的属性存在于 locations
中的文件之一中属性...
例如,如果我想做类似的事情:
@TestPropertySource(
locations = {"classpath:application.properties", "classpath:database.properties"},
properties = {"newPortNum = #{1 + Integer.parseInt(${myapp.web.server.port.ssl})}})
或这个:
@TestPropertySource(
locations = {"classpath:application.properties", "classpath:database.properties"},
properties = {"outputFile = ${outputDir}/foo.txt"})
我是否需要实现
TestExecutionListener
或 @BootstrapWith
完成这个?
最佳答案
直接来自 @TestPropertySource.locations()
的 Javadoc :
Property placeholders in paths (i.e.,
${...}
) will be resolved against theEnvironment
.
...这意味着:针对已添加到
Environment
的任何内容.另一方面,不支持 SpEL 表达式。
如果您需要编程支持来添加
PropertySource
到 Environment
,你应该实现一个 ApplicationContextInitializer
可以通过@ContextConfiguration(initializers = ...)
注册.问候,
Sam(Spring TestContext 框架的作者)
关于spring - @TestPropertySource 会尊重来自其他属性的 SPEL 或值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38003282/