Skip to content

Commit b133c2b

Browse files
committed
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
1 parent 648629f commit b133c2b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ public void afterEach(ExtensionContext context) throws Exception {
334334
* <li>The parameter is of type {@link ApplicationEvents} or a sub-type thereof.</li>
335335
* <li>{@link ParameterResolutionDelegate#isAutowirable} returns {@code true}.</li>
336336
* </ol>
337+
* <p>This method does not {@linkplain #getApplicationContext(ExtensionContext)
338+
* load} the {@code ApplicationContext} or verify that the application context
339+
* actually contains a matching candidate bean, since doing so would potentially
340+
* load an application context too early or unnecessarily.
337341
* <p><strong>WARNING</strong>: If a test class {@code Constructor} is annotated
338342
* with {@code @Autowired} or automatically autowirable (see
339343
* {@link org.springframework.test.context.TestConstructor @TestConstructor}),
@@ -392,6 +396,17 @@ private boolean supportsApplicationEvents(Class<?> parameterType, Executable exe
392396

393397
/**
394398
* Get the {@link ApplicationContext} associated with the supplied {@link ExtensionContext}.
399+
* <p><strong>NOTE</strong>: As of Spring Framework 7.0, the supplied
400+
* {@code ExtensionContext} may not be properly <em>scoped</em>. See the
401+
* {@linkplain SpringExtension class-level Javadoc} for further details.
402+
* <p><strong>WARNING</strong>: Invoking this method ensures that the
403+
* corresponding {@code ApplicationContext} is
404+
* {@linkplain org.springframework.test.context.TestContext#getApplicationContext()
405+
* loaded}. Consequently, this method should not be used if eager loading of
406+
* the application context is undesired. For example,
407+
* {@link #supportsParameter(ParameterContext, ExtensionContext)} intentionally
408+
* does not invoke this method, since doing so would potentially load an
409+
* application context too early or unnecessarily.
395410
* @param context the current {@code ExtensionContext} (never {@code null})
396411
* @return the application context
397412
* @throws IllegalStateException if an error occurs while retrieving the application context

0 commit comments

Comments
 (0)