From 7d5d48dc22d94aaf2b0d7352d4d85e794baae280 Mon Sep 17 00:00:00 2001 From: anqorithm <61029571+anqorithm@users.noreply.github.com> Date: Mon, 2 Nov 2026 16:19:06 +0100 Subject: [PATCH] Add @NestedTestConfiguration example to the testing docs Closes gh-28632 Signed-off-by: anqorithm <61029571+anqorithm@users.noreply.github.com> --- .../support-classes.adoc | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/framework-docs/modules/ROOT/pages/testing/testcontext-framework/support-classes.adoc b/framework-docs/modules/ROOT/pages/testing/testcontext-framework/support-classes.adoc index 9a051a16af03..970f75c8e355 100644 --- a/framework-docs/modules/ROOT/pages/testing/testcontext-framework/support-classes.adoc +++ b/framework-docs/modules/ROOT/pages/testing/testcontext-framework/support-classes.adoc @@ -492,6 +492,85 @@ Kotlin:: ---- ====== +If a `@Nested` test class should not inherit the configuration of its enclosing class, +annotate it with `@NestedTestConfiguration(EnclosingConfiguration.OVERRIDE)`. In +`OVERRIDE` mode, none of the Spring test annotations on the enclosing class are inherited, +including `@SpringJUnitConfig` and `@ContextConfiguration`, so the nested class has to +declare all of the configuration it needs. In the following example, `EnglishGreetings` +inherits the `@SpringJUnitConfig(TestConfig.class)` and `@ActiveProfiles("lang_en")` +configuration from `GreetingServiceTests`, while `FrenchGreetings` overrides both by +declaring its own configuration class and active profile. + +[tabs] +====== +Java:: ++ +[source,java,indent=0,subs="verbatim,quotes"] +---- + @SpringJUnitConfig(TestConfig.class) + @ActiveProfiles("lang_en") + class GreetingServiceTests { + + @Nested + class EnglishGreetings { + + @Test + void hello(@Autowired GreetingService service) { + assertThat(service.greetWorld()).isEqualTo("Hello World"); + } + } + + @Nested + @NestedTestConfiguration(EnclosingConfiguration.OVERRIDE) + @SpringJUnitConfig(FrenchConfig.class) + @ActiveProfiles("lang_fr") + class FrenchGreetings { + + @Test + void hello(@Autowired GreetingService service) { + assertThat(service.greetWorld()).isEqualTo("Bonjour le monde"); + } + } + } +---- + +Kotlin:: ++ +[source,kotlin,indent=0,subs="verbatim,quotes"] +---- + @SpringJUnitConfig(TestConfig::class) + @ActiveProfiles("lang_en") + class GreetingServiceTests { + + @Nested + inner class EnglishGreetings { + + @Test + fun hello(@Autowired service:GreetingService) { + assertThat(service.greetWorld()).isEqualTo("Hello World") + } + } + + @Nested + @NestedTestConfiguration(EnclosingConfiguration.OVERRIDE) + @SpringJUnitConfig(FrenchConfig::class) + @ActiveProfiles("lang_fr") + inner class FrenchGreetings { + + @Test + fun hello(@Autowired service:GreetingService) { + assertThat(service.greetWorld()).isEqualTo("Bonjour le monde") + } + } + } +---- +====== + +TIP: If you only need to replace some configuration, `OVERRIDE` mode is often more than +you want. For example, to keep the inherited configuration classes but use different +test properties, declare `@TestPropertySource(properties = ..., inheritProperties = false)` +on the `@Nested` class instead. + [[testcontext-junit4-support]] == JUnit 4 Support