From b133c2b346bf54d5f5c4df3e995f39605c13f7a3 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Sat, 15 Nov 2025 15:44:21 +0100 Subject: [PATCH] Document semantics of SpringExtension.getApplicationContext() Since getApplicationContext() was originally not intended to be part of the public API, its Javadoc is intentionally sparse. However, since it is actually a public API used by third parties, this commit improves the documentation for getApplicationContext() by pointing out that invoking the method actually results in the context being eagerly loaded, which may not be desired. This commit also updates the Javadoc for supportsParameter() along the same lines. Closes gh-35764 --- .../context/junit/jupiter/SpringExtension.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java b/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java index e9373a0026f..747a7f749d1 100644 --- a/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java +++ b/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java @@ -334,6 +334,10 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes *
  • The parameter is of type {@link ApplicationEvents} or a sub-type thereof.
  • *
  • {@link ParameterResolutionDelegate#isAutowirable} returns {@code true}.
  • * + *

    This method does not {@linkplain #getApplicationContext(ExtensionContext) + * load} the {@code ApplicationContext} or verify that the application context + * actually contains a matching candidate bean, since doing so would potentially + * load an application context too early or unnecessarily. *

    WARNING: If a test class {@code Constructor} is annotated * with {@code @Autowired} or automatically autowirable (see * {@link org.springframework.test.context.TestConstructor @TestConstructor}), @@ -392,6 +396,17 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes /** * Get the {@link ApplicationContext} associated with the supplied {@link ExtensionContext}. + *

    NOTE: As of Spring Framework 7.0, the supplied + * {@code ExtensionContext} may not be properly scoped. See the + * {@linkplain SpringExtension class-level Javadoc} for further details. + *

    WARNING: Invoking this method ensures that the + * corresponding {@code ApplicationContext} is + * {@linkplain org.springframework.test.context.TestContext#getApplicationContext() + * loaded}. Consequently, this method should not be used if eager loading of + * the application context is undesired. For example, + * {@link #supportsParameter(ParameterContext, ExtensionContext)} intentionally + * does not invoke this method, since doing so would potentially load an + * application context too early or unnecessarily. * @param context the current {@code ExtensionContext} (never {@code null}) * @return the application context * @throws IllegalStateException if an error occurs while retrieving the application context