spring-projects / spring-projects/spring-framework

Test subclass can not activate additional profiles if parent configures a context hierarchy

Open
#35,338 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: test status: waiting-for-triage
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

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:

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the BaseTest and Subtest examples from the issue, then read the linked context hierarchy and ActiveProfiles documentation. Trace the Spring TestContext handling for inherited @ContextHierarchy and @ActiveProfiles; done means the subclass activates both profiles without declaring @ContextConfiguration, with coverage for the reported hierarchy case.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.