我的集成测试设置如下:
@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中的值)。
然而这感觉不对,在我的测试中添加了这门课程的知识。我的问题是如何妥善解决这个问题?