-->

Spring boot integration test with SpringApplicatio

2019-07-11 17:32发布

问题:

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?