Skip to content

Commit bda8f20

Browse files
committed
Merge branch '6.2.x'
2 parents 3c14b42 + 82c34f7 commit bda8f20

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public void afterEach(ExtensionContext context) throws Exception {
281281
* <ol>
282282
* <li>The {@linkplain ParameterContext#getDeclaringExecutable() declaring
283283
* executable} is a {@link Constructor} and
284-
* {@link TestConstructorUtils#isAutowirableConstructor(Constructor, Class, PropertyProvider)}
284+
* {@link TestConstructorUtils#isAutowirableConstructor(Executable, PropertyProvider)}
285285
* returns {@code true}. Note that {@code isAutowirableConstructor()} will be
286286
* invoked with a fallback {@link PropertyProvider} that delegates its lookup
287287
* to {@link ExtensionContext#getConfigurationParameter(String)}.</li>
@@ -296,17 +296,16 @@ public void afterEach(ExtensionContext context) throws Exception {
296296
* constructor. Consequently, no other registered {@link ParameterResolver}
297297
* will be able to resolve parameters.
298298
* @see #resolveParameter
299-
* @see TestConstructorUtils#isAutowirableConstructor(Constructor, Class)
299+
* @see TestConstructorUtils#isAutowirableConstructor(Executable, PropertyProvider)
300300
* @see ParameterResolutionDelegate#isAutowirable
301301
*/
302302
@Override
303303
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
304304
Parameter parameter = parameterContext.getParameter();
305305
Executable executable = parameter.getDeclaringExecutable();
306-
Class<?> testClass = extensionContext.getRequiredTestClass();
307306
PropertyProvider junitPropertyProvider = propertyName ->
308307
extensionContext.getConfigurationParameter(propertyName).orElse(null);
309-
return (TestConstructorUtils.isAutowirableConstructor(executable, testClass, junitPropertyProvider) ||
308+
return (TestConstructorUtils.isAutowirableConstructor(executable, junitPropertyProvider) ||
310309
ApplicationContext.class.isAssignableFrom(parameter.getType()) ||
311310
supportsApplicationEvents(parameterContext) ||
312311
ParameterResolutionDelegate.isAutowirable(parameter, parameterContext.getIndex()));

spring-test/src/main/java/org/springframework/test/context/support/TestConstructorUtils.java

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public abstract class TestConstructorUtils {
6969
private TestConstructorUtils() {
7070
}
7171

72+
7273
/**
7374
* Determine if the supplied executable for the given test class is an
7475
* autowirable constructor.
@@ -77,8 +78,11 @@ private TestConstructorUtils() {
7778
* @param executable an executable for the test class
7879
* @param testClass the test class
7980
* @return {@code true} if the executable is an autowirable constructor
80-
* @see #isAutowirableConstructor(Executable, Class, PropertyProvider)
81+
* @see #isAutowirableConstructor(Executable, PropertyProvider)
82+
* @deprecated as of 6.2.13, in favor of {@link #isAutowirableConstructor(Executable, PropertyProvider)};
83+
* to be removed in Spring Framework 7.1
8184
*/
85+
@Deprecated(since = "6.2.13", forRemoval = true)
8286
public static boolean isAutowirableConstructor(Executable executable, Class<?> testClass) {
8387
return isAutowirableConstructor(executable, testClass, null);
8488
}
@@ -92,7 +96,10 @@ public static boolean isAutowirableConstructor(Executable executable, Class<?> t
9296
* @param testClass the test class
9397
* @return {@code true} if the constructor is autowirable
9498
* @see #isAutowirableConstructor(Constructor, Class, PropertyProvider)
99+
* @deprecated as of 6.2.13, in favor of {@link #isAutowirableConstructor(Executable, PropertyProvider)};
100+
* to be removed in Spring Framework 7.1
95101
*/
102+
@Deprecated(since = "6.2.13", forRemoval = true)
96103
public static boolean isAutowirableConstructor(Constructor<?> constructor, Class<?> testClass) {
97104
return isAutowirableConstructor(constructor, testClass, null);
98105
}
@@ -110,7 +117,10 @@ public static boolean isAutowirableConstructor(Constructor<?> constructor, Class
110117
* @return {@code true} if the executable is an autowirable constructor
111118
* @since 5.3
112119
* @see #isAutowirableConstructor(Constructor, Class, PropertyProvider)
120+
* @deprecated as of 6.2.13, in favor of {@link #isAutowirableConstructor(Executable, PropertyProvider)};
121+
* to be removed in Spring Framework 7.1
113122
*/
123+
@Deprecated(since = "6.2.13", forRemoval = true)
114124
public static boolean isAutowirableConstructor(Executable executable, Class<?> testClass,
115125
@Nullable PropertyProvider fallbackPropertyProvider) {
116126

@@ -138,16 +148,62 @@ public static boolean isAutowirableConstructor(Executable executable, Class<?> t
138148
* {@link TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME}).</li>
139149
* </ol>
140150
* @param constructor a constructor for the test class
141-
* @param testClass the test class
151+
* @param testClass the test class, typically the declaring class of the constructor
142152
* @param fallbackPropertyProvider fallback property provider used to look up
143-
* the value for the default <em>test constructor autowire mode</em> if no
144-
* such value is found in {@link SpringProperties}
153+
* the value for {@link TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME}
154+
* if no such value is found in {@link SpringProperties}; may be {@code null}
155+
* if there is no fallback support
145156
* @return {@code true} if the constructor is autowirable
146157
* @since 5.3
158+
* @see #isAutowirableConstructor(Executable, PropertyProvider)
159+
* @deprecated as of 6.2.13, in favor of {@link #isAutowirableConstructor(Executable, PropertyProvider)};
160+
* to be removed in Spring Framework 7.1
147161
*/
162+
@Deprecated(since = "6.2.13", forRemoval = true)
148163
public static boolean isAutowirableConstructor(Constructor<?> constructor, Class<?> testClass,
149164
@Nullable PropertyProvider fallbackPropertyProvider) {
150165

166+
return isAutowirableConstructorInternal(constructor, testClass, fallbackPropertyProvider);
167+
}
168+
169+
/**
170+
* Determine if the supplied {@link Executable} is an autowirable {@link Constructor}.
171+
*
172+
* <p>A constructor is considered to be autowirable if one of the following
173+
* conditions is {@code true}.
174+
*
175+
* <ol>
176+
* <li>The constructor is annotated with {@link Autowired @Autowired},
177+
* {@link jakarta.inject.Inject @jakarta.inject.Inject}, or
178+
* {@link javax.inject.Inject @javax.inject.Inject}.</li>
179+
* <li>{@link TestConstructor @TestConstructor} is <em>present</em> or
180+
* <em>meta-present</em> on the test class with
181+
* {@link TestConstructor#autowireMode() autowireMode} set to
182+
* {@link AutowireMode#ALL ALL}.</li>
183+
* <li>The default <em>test constructor autowire mode</em> has been set to
184+
* {@code ALL} in {@link SpringProperties} or in the supplied fallback
185+
* {@link PropertyProvider}.</li>
186+
* </ol>
187+
* @param executable an {@code Executable} for a test class
188+
* @param fallbackPropertyProvider fallback property provider used to look up
189+
* the value for {@value TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME}
190+
* if no such value is found in {@link SpringProperties}; may be {@code null}
191+
* if there is no fallback support
192+
* @return {@code true} if the executable is an autowirable constructor
193+
* @since 6.2.13
194+
* @see TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME
195+
*/
196+
public static boolean isAutowirableConstructor(Executable executable,
197+
@Nullable PropertyProvider fallbackPropertyProvider) {
198+
199+
return (executable instanceof Constructor<?> constructor &&
200+
isAutowirableConstructorInternal(constructor, constructor.getDeclaringClass(), fallbackPropertyProvider));
201+
}
202+
203+
204+
private static boolean isAutowirableConstructorInternal(Constructor<?> constructor, Class<?> testClass,
205+
@Nullable PropertyProvider fallbackPropertyProvider) {
206+
151207
// Is the constructor annotated with @Autowired/@Inject?
152208
if (isAnnotatedWithAutowiredOrInject(constructor)) {
153209
return true;

spring-test/src/test/java/org/springframework/test/context/support/TestConstructorUtilsTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
*/
4242
class TestConstructorUtilsTests {
4343

44+
private static final PropertyProvider propertyProvider = name -> null;
45+
46+
4447
@AfterEach
4548
void clearGlobalFlag() {
4649
setGlobalFlag(null);
@@ -100,12 +103,12 @@ void globalFlagVariations() throws Exception {
100103

101104
private void assertAutowirable(Class<?> testClass) throws NoSuchMethodException {
102105
Constructor<?> constructor = testClass.getDeclaredConstructor();
103-
assertThat(TestConstructorUtils.isAutowirableConstructor(constructor, testClass)).isTrue();
106+
assertThat(TestConstructorUtils.isAutowirableConstructor(constructor, propertyProvider)).isTrue();
104107
}
105108

106109
private void assertNotAutowirable(Class<?> testClass) throws NoSuchMethodException {
107110
Constructor<?> constructor = testClass.getDeclaredConstructor();
108-
assertThat(TestConstructorUtils.isAutowirableConstructor(constructor, testClass)).isFalse();
111+
assertThat(TestConstructorUtils.isAutowirableConstructor(constructor, propertyProvider)).isFalse();
109112
}
110113

111114
private void setGlobalFlag() {

0 commit comments

Comments
 (0)