Close

Spring Core Testing - Spring Core Testing - Using @NestedTestConfiguration

[Last Updated: Feb 6, 2026]

The @NestedTestConfiguration annotation is a JUnit 5 specific feature in the Spring TestContext framework. It determines whether a nested test class inherits configuration from its enclosing class.

Java source and doc

Definition of NestedTestConfiguration

Version: 7.0.3
 package org.springframework.test.context;
 @Target(ElementType.TYPE)
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 @Inherited
 public @interface NestedTestConfiguration {
     EnclosingConfiguration value(); 1
     enum EnclosingConfiguration { 2
         INHERIT, 3
         OVERRIDE 4
     }
 }
1Configures the EnclosingConfiguration mode.
2Enumeration of modes that dictate how test configuration from enclosing classes is processed for inner test classes.
3Indicates that test configuration for an inner test class should be inherited from its Class#getEnclosingClass(), as if the enclosing class were a superclass.
4Indicates that test configuration for an inner test class should override configuration from its Class#getEnclosingClass().

Why @NestedTestConfiguration is needed

With JUnit 5 nested tests, Spring must decide how application context configuration should be applied to nested test classes.

Specifically, Spring needs to determine whether configuration classes declared in an outer test class should automatically apply to inner (@Nested) test classes, or whether those nested classes should be treated as isolated test contexts.

The @NestedTestConfiguration annotation defines this rule explicitly.

The Two Supported Modes

@NestedTestConfiguration(EnclosingConfiguration.XXX)

1. INHERIT (Default)

EnclosingConfiguration.INHERIT

  • Nested test classes inherit configuration classes from enclosing test classes.
  • Configuration classes are merged.
  • A single hierarchical application context definition is created.

This is the default behavior.

2. OVERRIDE

EnclosingConfiguration.OVERRIDE

  • Nested test classes ignore configuration from enclosing test classes.
  • Only configuration declared directly on the nested test class is used.
  • Each nested test class can define an independent ApplicationContext.

Example

In the following example, the nested class successfully autowires a bean from the outer class configuration because inheritance is enabled.

package com.logicbig.example;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.NestedTestConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.springframework.test.context.NestedTestConfiguration.EnclosingConfiguration.INHERIT;
import static org.springframework.test.context.NestedTestConfiguration.EnclosingConfiguration.OVERRIDE;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = NestedConfigTest.Config.class)
// This tells all @Nested classes to inherit this context
@NestedTestConfiguration(INHERIT)
public class NestedConfigTest {

    @Configuration
    static class Config {
        @Bean
        public String parentBean() {
            return "Parent Context";
        }
    }

    @Autowired
    private String parentBean;

    @Test
    void outerTest() {
        assertNotNull(parentBean);
        System.out.println("Outer: " + parentBean);
    }

    @Nested
    class InnerTest {

        @Test
        void innerTest() {
            // This works because of @NestedTestConfiguration(INHERIT)
            assertNotNull(parentBean);
            System.out.println("Inner: " + parentBean);
        }
    }
}

Output

$ mvn test -Dtest="com.logicbig.example.NestedConfigTest"
[INFO] Scanning for projects...
[INFO]
[INFO] -----------< com.logicbig.example:nested-test-configuration >-----------
[INFO] Building nested-test-configuration 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ nested-test-configuration ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\example-projects\spring-core-testing\nested-test-configuration\src\main\resources
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ nested-test-configuration ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ nested-test-configuration ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\example-projects\spring-core-testing\nested-test-configuration\src\test\resources
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ nested-test-configuration ---
[INFO] Changes detected - recompiling the module! :source
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files with javac [debug target 25] to target\test-classes
[INFO]
[INFO] --- surefire:3.5.4:test (default-test) @ nested-test-configuration ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[WARNING] file.encoding cannot be set as system property, use <argLine>-Dfile.encoding=...</argLine> instead
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
Outer: Parent Context
Inner: Parent Context
[INFO] +--com.logicbig.example.NestedConfigTest - 0.030 ss
[INFO] | +-- [OK] outerTest - 0.040 ss
[INFO] | '-- [OK] innerTest - 0.005 ss
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.475 s
[INFO] Finished at: 2026-02-04T13:02:51+08:00
[INFO] ------------------------------------------------------------------------

Using EnclosingConfiguration.OVERRIDE

package com.logicbig.example;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.NestedTestConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.springframework.test.context.NestedTestConfiguration.EnclosingConfiguration.INHERIT;
import static org.springframework.test.context.NestedTestConfiguration.EnclosingConfiguration.OVERRIDE;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = NestedConfigTest2.Config.class)
// This tells all @Nested classes to override this context
@NestedTestConfiguration(OVERRIDE)
public class NestedConfigTest2 {

    @Configuration
    static class Config {
        @Bean
        public String parentBean() {
            return "Parent Context";
        }
    }

    @Autowired
    private String parentBean;

    @Test
    void outerTest() {
        assertNotNull(parentBean);
        System.out.println("Outer: " + parentBean);
    }

    @Nested
    class InnerTest {
        @Configuration
        static class Config {
            @Bean
            public String parentBean() {
                return "Parent Context2";
            }
        }

        @Test
        void innerTest() {
            // This works because of @NestedTestConfiguration(INHERIT)
            assertNotNull(parentBean);
            System.out.println("Inner: " + parentBean);
        }
    }
}

Output

$ mvn test -Dtest="com.logicbig.example.NestedConfigTest2"
[INFO] Scanning for projects...
[INFO]
[INFO] -----------< com.logicbig.example:nested-test-configuration >-----------
[INFO] Building nested-test-configuration 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ nested-test-configuration ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\example-projects\spring-core-testing\nested-test-configuration\src\main\resources
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ nested-test-configuration ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ nested-test-configuration ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\example-projects\spring-core-testing\nested-test-configuration\src\test\resources
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ nested-test-configuration ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- surefire:3.5.4:test (default-test) @ nested-test-configuration ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[WARNING] file.encoding cannot be set as system property, use <argLine>-Dfile.encoding=...</argLine> instead
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
Outer: Parent Context
Inner: Parent Context2
[INFO] +--com.logicbig.example.NestedConfigTest2 - 0.043 ss
[INFO] | +-- [OK] outerTest - 0.036 ss
[INFO] | '-- [OK] innerTest - 0.004 ss
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.571 s
[INFO] Finished at: 2026-02-04T13:03:01+08:00
[INFO] ------------------------------------------------------------------------

Conclusion

The output of the first example shows that the 'parentBean' is available in both the outer and inner test methods. This proves that @NestedTestConfiguration(INHERIT) allows the Spring context to permeate into inner classes, significantly reducing boilerplate code for complex test suites.

The output of the second example demonstrates the effect of @NestedTestConfiguration(OVERRIDE). While the outer class initializes with "Parent Context", the InnerTest ignores the enclosing class's configuration entirely and utilizes its own static Config class to produce "Parent Context2". This confirms that the OVERRIDE mode successfully breaks the default inheritance, providing a completely isolated ApplicationContext for the nested test suite.

Example Project

Dependencies and Technologies Used:

  • spring-context 7.0.3 (Spring Context)
     Version Compatibility: 5.3.0 - 7.0.3Version List
    ×

    Version compatibilities of spring-context with this example:

    • 5.3.0
    • 5.3.1
    • 5.3.2
    • 5.3.3
    • 5.3.4
    • 5.3.5
    • 5.3.6
    • 5.3.7
    • 5.3.8
    • 5.3.9
    • 5.3.10
    • 5.3.11
    • 5.3.12
    • 5.3.13
    • 5.3.14
    • 5.3.15
    • 5.3.16
    • 5.3.17
    • 5.3.18
    • 5.3.19
    • 5.3.20
    • 5.3.21
    • 5.3.22
    • 5.3.23
    • 5.3.24
    • 5.3.25
    • 5.3.26
    • 5.3.27
    • 5.3.28
    • 5.3.29
    • 5.3.30
    • 5.3.31
    • 5.3.32
    • 5.3.33
    • 5.3.34
    • 5.3.35
    • 5.3.36
    • 5.3.37
    • 5.3.38
    • 5.3.39
    • Compatible Java Version: 17+
    • 6.0.0
    • 6.0.1
    • 6.0.2
    • 6.0.3
    • 6.0.4
    • 6.0.5
    • 6.0.6
    • 6.0.7
    • 6.0.8
    • 6.0.9
    • 6.0.10
    • 6.0.11
    • 6.0.12
    • 6.0.13
    • 6.0.14
    • 6.0.15
    • 6.0.16
    • 6.0.17
    • 6.0.18
    • 6.0.19
    • 6.0.20
    • 6.0.21
    • 6.0.22
    • 6.0.23
    • 6.1.0
    • 6.1.1
    • 6.1.2
    • 6.1.3
    • 6.1.4
    • 6.1.5
    • 6.1.6
    • 6.1.7
    • 6.1.8
    • 6.1.9
    • 6.1.10
    • 6.1.11
    • 6.1.12
    • 6.1.13
    • 6.1.14
    • 6.1.15
    • 6.1.16
    • 6.1.17
    • 6.1.18
    • 6.1.19
    • 6.1.20
    • 6.1.21
    • 6.2.0
    • 6.2.1
    • 6.2.2
    • 6.2.3
    • 6.2.4
    • 6.2.5
    • 6.2.6
    • 6.2.7
    • 6.2.8
    • 6.2.9
    • 6.2.10
    • 6.2.11
    • 6.2.12
    • 6.2.13
    • 6.2.14
    • 6.2.15
    • 7.0.0
    • 7.0.1
    • 7.0.2
    • 7.0.3

    Versions in green have been tested.

  • spring-test 7.0.3 (Spring TestContext Framework)
  • junit-jupiter-engine 6.0.2 (Module "junit-jupiter-engine" of JUnit)
  • JDK 25
  • Maven 3.9.11

Spring Core Testing - Nested Test Configuration Inheritance Select All Download
  • nested-test-configuration
    • src
      • test
        • java
          • com
            • logicbig
              • example
                • NestedConfigTest.java

    See Also

    Join