spring-projects / spring-projects/spring-framework

`@Nested` test class does not reuse enclosing test class context (when no customization and `@ContextConfiguration`)

Open
#37,189 1 comment 0 reactions 1 assignee View on GitHub

@sbrannen is already working on this.

Since Aug 22, 2026.

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

Description

Since 7.1.0-M1 spring-test changes the logic calculating the MergedContextConfiguration entity for the nested test class. Consider example:

@SpringBootTest(classes = {
    EnclosingTest.Configuration.class
})
class EnclosingTest {
    class NestedTest {

Before 7.1.0-M1 both EnclosingTest and EnclosingTest.NestedTest had equal MergedContextConfiguration which also means that the same application context is reused between them.

But now it's changed. The MergedContextConfiguration are different, the nested class now has TWO classes in the classes fields (both the same).

Most probably it is related to this change between 7.0 and 7.1 (also doc):

List<ContextConfigurationAttributes> defaultConfigAttributesList =
        Collections.singletonList(new ContextConfigurationAttributes(testClass));
        // for 7.1: ContextLoaderUtils.resolveDefaultContextConfigurationAttributes(testClass);
Crucial detail

After some research it was found, that this depends on the presence of @ContextConfiguration on the enclosing class. If there is a declaration like this:

@SpringBootTest(classes = {
    EnclosingTest.Configuration.class
})
@ContextConfiguration
class EnclosingTest {
    class NestedTest {

then the test cache behaves as expected.

Most probably it works correctly because of this condition in AbstractTestContextBootstrapper:

@Override
public final MergedContextConfiguration buildMergedContextConfiguration() {
...
// is evaluated to false (annotation found) if @ContextConfiguration is present, and the branch skipped
	if (TestContextAnnotationUtils.findAnnotationDescriptorForTypes(
			testClass, ContextConfiguration.class, ContextHierarchy.class) == null) {
		return buildDefaultMergedContextConfiguration(testClass, cacheAwareContextLoaderDelegate);
	}

So, the question is: is it a desired behavior for @SpringBootTest (which is not a meta-annotation of @ContextConfiguration), or it's a bug?

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.