2

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?

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
IHaanstra
  • 196
  • 1
  • 9
  • By providing `SpringApplicationConfiguration` with the proper class. It should be your application class, not an arbitrary class from your classpath. – M. Deinum Jun 28 '16 at 12:41
  • Thanks for your quick reply. With the proper class, you mean the class with the @SpringBootApplication annotation? I can see how that makes sense however we like to do a kind of integration test on a class used lower in the chain. The main class does some work we don't want to test there (like determine input dir, output dir, correct input and output filenames) and we only want here to test the part where a input filename and output names are explicitely supplied. – IHaanstra Jun 28 '16 at 13:12
  • Then don't use `SpringApplicationConfiguration`... Or add all the configs you need (like you are doing now). But basically that is not what it was intended for... – M. Deinum Jun 28 '16 at 13:26
  • But isn't wha you really want a unit test and not an integration test? – M. Deinum Jun 28 '16 at 13:35
  • It is a test of a method of an object that has autowired other objects which have autowired other objects. Without the `SpringApplicationConfiguration` we would have to set the object tree ourselves. We could do that but i liked this easy way :-) – IHaanstra Jun 29 '16 at 06:57

0 Answers0