Spring boot integration test with SpringApplicationConfiguration doesn't seem to resolve @Value annotation

点点圈 提交于 2019-12-24 01:37:14

问题


I have an integration test set up like:

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

In the XmlFileSplitter I have a property that is annotated with @Value("${default.output.file}") and its value is retrieved from the application.properties. This works fine when running the app normally. However when running the integration test, the value is not resolved (it is "${default.output.file}"). When i debugged through the code for resolving the placeholder i noticed the org.springframework.beans.factory.support.AbstractBeanFactory embeddedValueResolvers was empty in the test, while containing a PropertySourcesPlaceholderConfigurer when running the app normally.

I saw the normal run has PropertyPlaceholderAutoConfiguration from spring-boot-autoconfigure to configure the propertyplaceholder and i figured i needed to add this class to the SpringApplicationConfiguration classes to have it configured for the integration test. I added it:

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

and it now indeeds resolves the @Value annotation (with value from application.properties).

However this feels wrong, adding knowledge of this class to my test. My question is how to solve this properly?

来源:https://stackoverflow.com/questions/38075878/spring-boot-integration-test-with-springapplicationconfiguration-doesnt-seem-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!