Skip to content

Commit 9c24f7b

Browse files
committed
Document test-method scoped TestContext semantics
Although gh-35680 introduced support for JUnit Jupiter's TEST_METHOD ExtensionContextScope in the SpringExtension and BeanOverrideTestExecutionListener, those were internal changes that are not directly visible by TestExecutionListener authors. However, in order to be compatible with a test-method scoped TestContext (which takes its values from the current Jupiter ExtensionContext), existing third-party TestExecutionListener implementations may need to be modified similar to how BeanOverrideTestExecutionListener was modified in commit d24a31d. To raise awareness of how to deal with such issues, this commit documents test-method TestContext semantics for TestExecutionListener authors. Closes gh-35716
1 parent a40647a commit 9c24f7b

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

spring-test/src/main/java/org/springframework/test/context/TestContext.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ default void publishEvent(Function<TestContext, ? extends ApplicationEvent> even
9898

9999
/**
100100
* Get the {@linkplain Class test class} for this test context.
101+
* <p>Since JUnit Jupiter 5.12, if the
102+
* {@link org.springframework.test.context.junit.jupiter.SpringExtension
103+
* SpringExtension} is used with a {@linkplain
104+
* org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope#TEST_METHOD
105+
* test-method scoped} {@link org.junit.jupiter.api.extension.ExtensionContext
106+
* ExtensionContext}, the {@code Class} returned from this method may refer
107+
* to the test class for the current {@linkplain #getTestMethod() test method},
108+
* which may be a {@link org.junit.jupiter.api.Nested @Nested} test class
109+
* within the class for the {@linkplain #getTestInstance() test instance}.
110+
* Thus, if you need consistent access to the class for the current test
111+
* instance within an implementation of
112+
* {@link TestExecutionListener#prepareTestInstance(TestContext)}, you should
113+
* invoke {@code testContext.getTestInstance().getClass()} instead of
114+
* {@code testContext.getTestClass()}.
101115
* @return the test class (never {@code null})
102116
*/
103117
Class<?> getTestClass();

spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ default void beforeTestClass(TestContext testContext) throws Exception {
150150
* {@link org.springframework.test.context.junit4.rules.SpringMethodRule
151151
* SpringMethodRule}). In any case, this method must be called prior to any
152152
* framework-specific lifecycle callbacks.
153+
* <p>Since JUnit Jupiter 5.12, if the
154+
* {@link org.springframework.test.context.junit.jupiter.SpringExtension
155+
* SpringExtension} is used with a {@linkplain
156+
* org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope#TEST_METHOD
157+
* test-method scoped} {@link org.junit.jupiter.api.extension.ExtensionContext
158+
* ExtensionContext}, the {@link Class} returned from
159+
* {@link TestContext#getTestClass()} may refer to the test class for the
160+
* current {@linkplain TestContext#getTestMethod() test method}, which may be
161+
* a {@link org.junit.jupiter.api.Nested @Nested} test class within the class
162+
* for the {@linkplain TestContext#getTestInstance() test instance}. Thus, if
163+
* you need consistent access to the class for the current test instance, you
164+
* should invoke {@code testContext.getTestInstance().getClass()} instead of
165+
* {@code testContext.getTestClass()}.
153166
* <p>See the {@linkplain TestExecutionListener class-level documentation}
154167
* for details on wrapping behavior for listeners.
155168
* <p>The default implementation is <em>empty</em>. Can be overridden by

0 commit comments

Comments
 (0)