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")));
}
}
}
OutputD:\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 ProjectDependencies and Technologies Used: - spring-context 7.0.3 (Spring Context)
Version Compatibility: 6.0.0 - 7.0.3 Version compatibilities of spring-context with this example: 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
|