spring - @TestPropertySource 会尊重来自其他属性的 SPEL 或值吗?

标签 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完成这个?

最佳答案

直接来自 @TestPropertySource.locations() 的 Javadoc :

Property placeholders in paths (i.e., ${...}) will be resolved against the Environment.



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

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

如果您需要编程支持来添加 PropertySourceEnvironment ,你应该实现一个 ApplicationContextInitializer可以通过@ContextConfiguration(initializers = ...)注册.

问候,

Sam(Spring TestContext 框架的作者)

关于spring - @TestPropertySource 会尊重来自其他属性的 SPEL 或值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38003282/

相关文章:

java - Spring 测试多次关闭嵌入式数据库

java - 无法在 Spring Boot 中为每个测试方法初始化数据库

java - spring-boot 在多模块应用程序上失败

spring - 如何使用来自 spring-data-rest 的实体响应列表

java - 如何将 PostgreSQL hstore/json 与 JdbcTemplate 一起使用

java - 二级缓存 + Spring Boot + Java Hibernate 中的问题

java - 在测试中的 Spring 环境中使用 @EnabledIf 和 spring.profiles.active 属性

java - Powermock ExceptionInInitializerError 与 Junit

java - validator 从 Spring-boot 中的 Bean 检索属性值(BeanUtils 的替代方案)

java - Spring Boot请求方法 'POST'不支持