@TestPropertySource是否会尊重SPEL或其他属性的值?

时间:2016-06-23 23:05:31

标签: spring spring-boot spring-test

我想知道@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才能完成此任务?

1 个答案:

答案 0 :(得分:3)

直接从Javadoc @TestPropertySource.locations()

  

路径中的属性占位符(即${...})将根据Environment进行解析。

......这意味着:针对已添加到Environment的任何内容。

另一方面,不支持SpEL表达式。

如果您需要编程支持向PropertySource添加Environment,则应实施ApplicationContextInitializer,可以通过@ContextConfiguration(initializers = ...)注册。

此致

Sam (Spring TestContext Framework的作者)

相关问题