5

Using Spring Boot 1.5.16 in WebMvcTest I'm trying to load test.properties applying @TestPropertySource annotation in order to override some properties in test class. This works just fine if I put it on test class:

@RunWith(SpringRunner.class) @WebMvcTest @TestPropertySource("classpath:test.properties") public class ControllerTest { ... } 

But properties not loaded if I move it to imported configuration:

@RunWith(SpringRunner.class) @WebMvcTest @Import(ControllersConfiguration.class) public class ControllerTest { ... } 

and ControllersConfiguration class is:

@TestConfiguration @TestPropertySource("classpath:test.properties") public class ControllersConfiguration { ... } 

Can you explain that behavior?

P.S. @PropertySource annotation is working in imported configuration, but with lowest precedence than application.properties

UPD: To be clear - try to make all test pass here: https://github.com/Javasick/WeirdTestPropertySource

2
  • have you try to put inheritLocations for @TestPropertySource as false? Commented Dec 3, 2018 at 13:33
  • Yes, doesn't help Commented Dec 3, 2018 at 14:04

2 Answers 2

5

I investigated it yesterday and found that Spring is looking for this @TestPropertySource annotation only on:

  • Source test class
  • Interfaces if test class implements them
  • Superclases of this test class
  • Inherited annotations

Here part of code from AbstractTestContextBootstrapper.class that responsible for it:

MergedTestPropertySources mergedTestPropertySources = TestPropertySourceUtils.buildMergedTestPropertySources(testClass); MergedContextConfiguration mergedConfig = new MergedContextConfiguration(testClass, StringUtils.toStringArray(locations), ClassUtils.toClassArray(classes), ApplicationContextInitializerUtils.resolveInitializerClasses(configAttributesList), ActiveProfilesUtils.resolveActiveProfiles(testClass), mergedTestPropertySources.getLocations(), mergedTestPropertySources.getProperties(), contextCustomizers, contextLoader, cacheAwareContextLoaderDelegate, parentConfig); 

The method TestPropertySourceUtils.buildMergedTestPropertySources(testClass) exactly responsible for finding and extracting location from this annotation. As you can see Spring invoke it only on test class.

So if you want to externalize this annotation you need to create super class and put this annotation and @Import on it, or create interface with this annotation, or create your own annotation that will combine two annotation @Import and @TestPropertySource and put it on your test classes.

Sign up to request clarification or add additional context in comments.

2 Comments

Thx, That the answer I was looking for
Can you give an example for the "Inherited annotations"?
-1

From JavaDocs:

Enabling @TestPropertySource @TestPropertySource is enabled if the configured context loader honors it.

Every SmartContextLoader that is a subclass of either AbstractGenericContextLoader or AbstractGenericWebContextLoader provides automatic support for @TestPropertySource, and this includes every SmartContextLoader provided by the Spring TestContext Framework.

Bottom line is:

Typically, @TestPropertySource will be used in conjunction with @ContextConfiguration.

So you should annotate your Test class with @ContextConfiguration

2 Comments

I've tried to put @ContextConfiguration and it does not work
BTW, Test is working, but putting properties in imported configuration - not

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.