【问题标题】:@ActiveProfiles in Spring Test does not support placeholders any longerSpring Test 中的@ActiveProfiles 不再支持占位符
【发布时间】:2021-01-27 17:46:10
【问题描述】:

我正在从 Spring 4.3 升级到 Spring 5.3,似乎 @ActiveProviles 注释不再支持占位符。

以下代码适用于旧的 Spring 版本:

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles({"${profileA}","someProfileWithoutPlaceholders"})
@ContextConfiguration(classes = MyApplication.class)
public class MyTest {...}

但它停止了升级,现在它得到了

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'nucleus.hibernate.dialect' in value "${nucleus.hibernate.dialect}"
        at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178)
        at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)
        at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239)
        at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
        at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175)
        at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:936)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1321)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
        ... 55 more

请注意,“nucleus.hibernate.dialect”是在“${profileA}”引用的应用程序属性中定义的。

作为一种解决方法,我尝试使用显式将属性指定为测试属性源

@TestPropertySource(locations={"classpath:/application-${profileA}.properties"}})

这行得通。

我不确定在 Spring 集成测试中使用占位符选择 Spring 配置文件是否是官方支持的功能。如果是,我认为这是 Spring 测试框架中的重大变化。

【问题讨论】:

    标签: placeholder spring-test spring-boot-2 spring5


    【解决方案1】:

    在围绕测试用例进行调试之后,我对这个问题有了更多的了解。

    好像在

    org.springframework.core.env.PropertySourcesPropertyResolver#getProperty(java.lang.String, java.lang.Class<T>, boolean)
    

    测试属性源“测试”拆分配置文件并有两个条目:

    spring.profiles.active[0] -> "${profileA}
    spring.profiles.active[1] -> "someProfileWithoutPlaceholders"
    

    所以当用键“spring.profiles.active”查找时,没有找到任何值,也没有调用org.springframework.core.env.AbstractPropertyResolver#resolveNestedPlaceholders

    这是在构建测试应用程序上下文时在org.springframework.core.env.AbstractEnvironment#doGetActiveProfiles 中完成的。

    在旧的 Spring 版本中,只有一个地图条目:

    spring.profiles.active -> "${profileA},someProfileWithoutPlaceholders"
    

    所以它找到了条目并解析了占位符。

    【讨论】:

      猜你喜欢
      • 2021-10-27
      • 1970-01-01
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      相关资源
      最近更新 更多