During testing and setting up I noticed a wierd bug. Namely if a test class extends some base test class, which has a @ContextHierarchy
annotation applied, then the subclass can not activate additional profiles without at least declaring @ContextConfiguration
.
For example the following setup works:
@SpringBootTest
@ActiveProfiles("base")
public class BaseTest {
}
@ActiveProfiles("sub")
public class Subtest extends BaseTest {
@Autowired
private Environment environment;
@Test
void activatesProfiles() {
assertThat(environment.getActiveProfiles()).containsExactlyInAnyOrder("base", "sub");
}
}
but if the superclass is changed, to declare context hieararchy, then it stops working. For example following does not work anymore:
@ContextHierarchy(@ContextConfiguration)
@SpringBootTest
@ActiveProfiles("base")
public class BaseTest {
}
@ActiveProfiles("sub")
public class Subtest extends BaseTest {
@Autowired
private Environment environment;
@Test
void activatesProfiles() {
assertThat(environment.getActiveProfiles()).containsExactlyInAnyOrder("base", "sub");
}
}
Furthermore if the subclass is changed to have it's own @ContextConfiguration
the whole setup works again:
@ContextHierarchy(@ContextConfiguration)
@SpringBootTest
@ActiveProfiles("base")
public class BaseTest {
}
@ActiveProfiles("sub")
@ContextConfiguration
public class Subtest extends BaseTest {
@Autowired
private Environment environment;
@Test
void activatesProfiles() {
assertThat(environment.getActiveProfiles()).containsExactlyInAnyOrder("base", "sub");
}
}
It was my expectation that the subclass will not require any additional overriding of configurations, which come from parent class, while still allowing me to define additional profiles. Further the documentation is not clear on this matter, as can be seen in:
https://docs.spring.io/spring-framework/reference/testing/testcontext-framework/ctx-management/hierarchies.html
https://docs.spring.io/spring-framework/reference/testing/annotations/integration-spring/annotation-activeprofiles.html
https://docs.spring.io/spring-framework/reference/testing/testcontext-framework/ctx-management/env-profiles.html
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/ActiveProfiles.html
Comment From: mantis-sa
Hey @thecooldrop ,
The purpose of @ContextHierarchy
is to define a hierarchy of ApplicationContext
for integration tests (See first line here).
The 'bug' usecase you are describing produces an assertion error claiming that it could only find base
. The reason this test is running at all is because it inherits from the BaseTest
- it's the only definition of an ApplicationContext
in this hierarchy (you can test this by removing the extends BaseTest
and run the test - the test throws an NPE on the getActiveProfiles
because there is no Environment
-> there is no Environment
because there is no ApplicationContext
). As a result, your hierarchy in this scenario is a null
context trying to inherit from the ApplicationContext
defined in BaseTest
.
So, imo, the pre-requisite for @ContextHierarchy
should be that all classes in the hierarchy define some sort of context.
And if the claim is that in situations where a context is not defined in the hierarchy, it should just 'absorb' the null context into one defined into the hierarchy - I would argue that this could cause phantom issues for developers, and that it's much better to explicitly require developers to define their Context.
In summary, I think the documentation is explicit enough.