Close

Spring Core Testing - Active profiles in @Nested JUnit 5 test classes

[Last Updated: Feb 2, 2026]

In JUnit 5, test classes can contain @Nested inner classes to group related scenarios. When using Spring's @ActiveProfiles on nested classes, Spring's TestContext framework evaluates profiles for each nested context: the nested class can inherit the outer class profiles (inheritProfiles = true, the default) or completely override them (inheritProfiles = false).

Use cases:

  • Model multiple environment setups within a single test class (e.g., dev vs prod).
  • Keep a common baseline (e.g., base) in the outer class and selectively add/replace profiles in nested classes.
  • Reduce duplication by sharing configuration and context initialization across related scenarios.

Example

Configuration

package com.logicbig.example;

import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
    // No beans needed for this demo; we only assert Environment active profiles.
}

JUnit 5 nested tests with profiles

package com.logicbig.example;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringJUnitConfig(classes = AppConfig.class)
@ActiveProfiles("base")
public class NestedProfileTest {

    @Autowired
    private Environment environment;

    @Test
    public void testOuterProfiles() {
        String[] profiles = environment.getActiveProfiles();
        System.out.println("Outer profiles: " + java.util.Arrays.toString(profiles));
        // Output: Outer profiles: [base]
        assertTrue(environment.acceptsProfiles(Profiles.of("base")));
        assertFalse(environment.acceptsProfiles(Profiles.of("dev")));
        assertFalse(environment.acceptsProfiles(Profiles.of("prod")));
    }

    @Nested
    @ActiveProfiles(value = "dev", inheritProfiles = true)
    class DevEnvironmentTests {

        @Autowired
        private Environment environment;

        @Test
        public void testDevProfiles() {
            String[] profiles = environment.getActiveProfiles();
            System.out.println("Dev profiles: " + java.util.Arrays.toString(profiles));
            // Output: Dev profiles: [base, dev]
            assertTrue(environment.acceptsProfiles(Profiles.of("base & dev")));
        }
    }

    @Nested
    @ActiveProfiles(value = "prod", inheritProfiles = false)
    class ProdEnvironmentTests {

        @Autowired
        private Environment environment;

        @Test
        public void testProdProfiles() {
            String[] profiles = environment.getActiveProfiles();
            System.out.println("Prod profiles: " + java.util.Arrays.toString(profiles));
            // Output: Prod profiles: [prod]
            assertFalse(environment.acceptsProfiles(Profiles.of("base")));
            assertTrue(environment.acceptsProfiles(Profiles.of("prod")));
        }
    }
}

Output

D:\example-projects\spring-core-testing\spring-active-profiles-nested-classes-example>mvn test -Dtest=NestedProfileTest
[INFO] Scanning for projects...
[INFO]
[INFO] --< com.logicbig.example:spring-active-profiles-nested-classes-example >--
[INFO] Building spring-active-profiles-nested-classes-example 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ spring-active-profiles-nested-classes-example ---
[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\spring-active-profiles-nested-classes-example\src\main\resources
[INFO]
[INFO] --- compiler:3.14.1:compile (default-compile) @ spring-active-profiles-nested-classes-example ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ spring-active-profiles-nested-classes-example ---
[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\spring-active-profiles-nested-classes-example\src\test\resources
[INFO]
[INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ spring-active-profiles-nested-classes-example ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- surefire:3.5.0:test (default-test) @ spring-active-profiles-nested-classes-example ---
[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 profiles: [base]
Dev profiles: [base, dev]
Prod profiles: [prod]
[INFO] +--com.logicbig.example.NestedProfileTest - 0.286 ss
[INFO] | '-- [OK] testOuterProfiles - 0.021 ss
[INFO] +--.--com.logicbig.example.NestedProfileTest$DevEnvironmentTests - 0.021 ss
[INFO] | | '-- [OK] testDevProfiles - 0.003 ss
[INFO] +-----com.logicbig.example.NestedProfileTest$ProdEnvironmentTests - 0.021 ss
[INFO] | '-- [OK] testProdProfiles - 0.004 ss
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.042 s
[INFO] Finished at: 2026-01-31T00:17:51+08:00
[INFO] ------------------------------------------------------------------------

The outer class activates base. The DevEnvironmentTests nested class adds dev with inheritance enabled, resulting in [base, dev]. The ProdEnvironmentTests nested class disables inheritance and activates only prod. Assertions use Environment#acceptsProfiles(Profiles.of(...)) and getActiveProfiles(). No application code is required; everything resides under src/test/java.

In above example we used JUnit5StatelessTestsetInfoTreeReporter to get the formatted output as tree (see pom.xml in project browser).

Example Project

Dependencies and Technologies Used:

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

    Version compatibilities of spring-context with this example:

      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 - Active profiles in @Nested classes Select All Download
  • spring-active-profiles-nested-classes-example
    • src
      • test
        • java
          • com
            • logicbig
              • example
                • NestedProfileTest.java

    See Also

    Join