SpringApplicationConfiguration的Spring启动集成测试似乎无法解析@Value注释

时间:2016-06-28 12:10:00

标签: java spring spring-boot property-placeholder

我的集成测试设置如下:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {XmlFileSplitter.class, ...})
public class XmlFileSplitterTests { ..

XmlFileSplitter我有一个使用@Value("${default.output.file}")注释的属性,其值从application.properties中检索。这通常正常运行应用程序时工作正常。但是,在运行集成测试时,不会解析该值(它是“${default.output.file}”)。 当我通过解析占位符的代码调试时,我注意到测试中org.springframework.beans.factory.support.AbstractBeanFactory embeddedValueResolvers为空,而正常运行应用时包含PropertySourcesPlaceholderConfigurer

我看到spring-boot-autoconfigure的正常运行有PropertyPlaceholderAutoConfiguration来配置propertyplaceholder,我想我需要将这个类添加到SpringApplicationConfiguration类中,以便为集成测试配置它。我补充说:

@SpringApplicationConfiguration(classes = {XmlFileSplitter.class, ... , PropertyPlaceholderAutoConfiguration.class})

现在,indets会解析@Value注释(使用application.properties中的值)。

然而这感觉不对,在我的测试中添加了这门课程的知识。我的问题是如何妥善解决这个问题?

0 个答案:

没有答案