Skip to content

Commit 82c34f7

Browse files
committed
Introduce isAutowirableConstructor(Executable,PropertyProvider) in TestConstructorUtils
This commit introduces a new isAutowirableConstructor(Executable, PropertyProvider) overload in TestConstructorUtils and deprecates all other existing variants for removal in 7.1. Closes gh-35676
1 parent cb0f26a commit 82c34f7

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
@@ -282,7 +282,7 @@ public void afterEach(ExtensionContext context) throws Exception {
282282
* <ol>
283283
* <li>The {@linkplain ParameterContext#getDeclaringExecutable() declaring
284284
* executable} is a {@link Constructor} and
285-
* {@link TestConstructorUtils#isAutowirableConstructor(Constructor, Class, PropertyProvider)}
285+
* {@link TestConstructorUtils#isAutowirableConstructor(Executable, PropertyProvider)}
286286
* returns {@code true}. Note that {@code isAutowirableConstructor()} will be
287287
* invoked with a fallback {@link PropertyProvider} that delegates its lookup
288288
* 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
@@ -78,6 +78,7 @@ public abstract class TestConstructorUtils {
7878
private TestConstructorUtils() {
7979
}
8080

81+
8182
/**
8283
* Determine if the supplied executable for the given test class is an
8384
* autowirable constructor.
@@ -86,8 +87,11 @@ private TestConstructorUtils() {
8687
* @param executable an executable for the test class
8788
* @param testClass the test class
8889
* @return {@code true} if the executable is an autowirable constructor
89-
* @see #isAutowirableConstructor(Executable, Class, PropertyProvider)
90+
* @see #isAutowirableConstructor(Executable, PropertyProvider)
91+
* @deprecated as of 6.2.13, in favor of {@link #isAutowirableConstructor(Executable, PropertyProvider)};
92+
* to be removed in Spring Framework 7.1
9093
*/
94+
@Deprecated(since = "6.2.13", forRemoval = true)
9195
public static boolean isAutowirableConstructor(Executable executable, Class<?> testClass) {
9296
return isAutowirableConstructor(executable, testClass, null);
9397
}
@@ -101,7 +105,10 @@ public static boolean isAutowirableConstructor(Executable executable, Class<?> t
101105
* @param testClass the test class
102106
* @return {@code true} if the constructor is autowirable
103107
* @see #isAutowirableConstructor(Constructor, Class, PropertyProvider)
108+
* @deprecated as of 6.2.13, in favor of {@link #isAutowirableConstructor(Executable, PropertyProvider)};
109+
* to be removed in Spring Framework 7.1
104110
*/
111+
@Deprecated(since = "6.2.13", forRemoval = true)
105112
public static boolean isAutowirableConstructor(Constructor<?> constructor, Class<?> testClass) {
106113
return isAutowirableConstructor(constructor, testClass, null);
107114
}
@@ -119,7 +126,10 @@ public static boolean isAutowirableConstructor(Constructor<?> constructor, Class
119126
* @return {@code true} if the executable is an autowirable constructor
120127
* @since 5.3
121128
* @see #isAutowirableConstructor(Constructor, Class, PropertyProvider)
129+
* @deprecated as of 6.2.13, in favor of {@link #isAutowirableConstructor(Executable, PropertyProvider)};
130+
* to be removed in Spring Framework 7.1
122131
*/
132+
@Deprecated(since = "6.2.13", forRemoval = true)
123133
public static boolean isAutowirableConstructor(Executable executable, Class<?> testClass,
124134
@Nullable PropertyProvider fallbackPropertyProvider) {
125135

@@ -148,16 +158,62 @@ public static boolean isAutowirableConstructor(Executable executable, Class<?> t
148158
* {@link TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME}).</li>
149159
* </ol>
150160
* @param constructor a constructor for the test class
151-
* @param testClass the test class
161+
* @param testClass the test class, typically the declaring class of the constructor
152162
* @param fallbackPropertyProvider fallback property provider used to look up
153-
* the value for the default <em>test constructor autowire mode</em> if no
154-
* such value is found in {@link SpringProperties}
163+
* the value for {@link TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME}
164+
* if no such value is found in {@link SpringProperties}; may be {@code null}
165+
* if there is no fallback support
155166
* @return {@code true} if the constructor is autowirable
156167
* @since 5.3
168+
* @see #isAutowirableConstructor(Executable, PropertyProvider)
169+
* @deprecated as of 6.2.13, in favor of {@link #isAutowirableConstructor(Executable, PropertyProvider)};
170+
* to be removed in Spring Framework 7.1
157171
*/
172+
@Deprecated(since = "6.2.13", forRemoval = true)
158173
public static boolean isAutowirableConstructor(Constructor<?> constructor, Class<?> testClass,
159174
@Nullable PropertyProvider fallbackPropertyProvider) {
160175

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