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.