-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
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: