1

When using @TestPropertySource in Spring Boot both directly on a test class and in addition on a meta annotation, the properties of the two property sources are not being merged, but instead only the properties defined on the test class are present.

The JUnit 5 test:

@CustomTest
@TestPropertySource(properties = "property.b=value-b")
class TestPropertySourceTests {

    @Autowired
    private Environment environment;

    @Test
    void testPropertiesFromTestPropertySourcesAreMergedFromTestAndMetaAnnotation() {
        assertThat(environment.getProperty("property.a"), is(equalTo("value-a")));
        assertThat(environment.getProperty("property.b"), is(equalTo("value-b")));
    }

}

The meta annotation:

@Retention(RUNTIME)
@ExtendWith(SpringExtension.class)
@SpringBootTest
@TestPropertySource(properties = "property.a=value-a")
@interface CustomTest {
}

The test fails with property.a being null instead of value-a, showing that the properties defined via the meta annotation are not present.

The same works if the two @TestPropertySource annotations would in a class hierarchy (i.e. TestPropertySourceTests would extend from BaseTests which in turns defines property.a in its own @TestPropertySource)

Is this intended (and documented) behaviour? One could argue that the order is only defined in class hierarchies, but not in trees of annotations, or between class hierarchies and trees of annotations.

cstettler
  • 11
  • 1

2 Answers2

0

This is because when Spring bootstraps your Test Context it will only search for local decalared annotations of Type org.springframework.test.context.TestPropertySource.

Have a look into TestPropertySourceUtils here and also have a look into MetaAnnotationUtils in here

    if (AnnotationUtils.isAnnotationDeclaredLocally(annotationType, clazz)) {
        return new AnnotationDescriptor<>(clazz, clazz.getAnnotation(annotationType));
    }

As they return immediately the result, there is no chance to also fetch your @CustomTest annotation with its declared @TestPropertySource annotation.

So it is currently not possible.

mrkernelpanic
  • 4,268
  • 4
  • 28
  • 52
  • I agree that this is the current behaviour (as observed), but I was wondering whether this has a specific reason. I am hoping for a statement similar to "this would not make sense at all because of ..." in order to understand whether this is "just the case" or whether this has been implemented that way intentionally. – cstettler Aug 15 '18 at 11:26
0

This is by design in Spring. Sam Brannen confirmed this in other answers:

I opened an issue in Spring's tracker: https://github.com/spring-projects/spring-framework/issues/23299

Please vote for it if you still need the functionality.

Gebezs
  • 696
  • 3
  • 9