Introduce isAutowirableConstructor(Executable,PropertyProvider) in TestConstructorUtils
Backport Bot / build (push) Waiting to run
Details
Build and Deploy Snapshot / Build and Deploy Snapshot (push) Waiting to run
Details
Build and Deploy Snapshot / Verify (push) Blocked by required conditions
Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:false version:17], map[id:ubuntu-latest name:Linux]) (push) Waiting to run
Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:21], map[id:ubuntu-latest name:Linux]) (push) Waiting to run
Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:25], map[id:ubuntu-latest name:Linux]) (push) Waiting to run
Details
Deploy Docs / Dispatch docs deployment (push) Waiting to run
Details
Backport Bot / build (push) Waiting to run
Details
Build and Deploy Snapshot / Build and Deploy Snapshot (push) Waiting to run
Details
Build and Deploy Snapshot / Verify (push) Blocked by required conditions
Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:false version:17], map[id:ubuntu-latest name:Linux]) (push) Waiting to run
Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:21], map[id:ubuntu-latest name:Linux]) (push) Waiting to run
Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:25], map[id:ubuntu-latest name:Linux]) (push) Waiting to run
Details
Deploy Docs / Dispatch docs deployment (push) Waiting to run
Details
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
This commit is contained in:
parent
cb0f26a152
commit
82c34f7b51
|
|
@ -282,7 +282,7 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
|||
* <ol>
|
||||
* <li>The {@linkplain ParameterContext#getDeclaringExecutable() declaring
|
||||
* executable} is a {@link Constructor} and
|
||||
* {@link TestConstructorUtils#isAutowirableConstructor(Constructor, Class, PropertyProvider)}
|
||||
* {@link TestConstructorUtils#isAutowirableConstructor(Executable, PropertyProvider)}
|
||||
* returns {@code true}. Note that {@code isAutowirableConstructor()} will be
|
||||
* invoked with a fallback {@link PropertyProvider} that delegates its lookup
|
||||
* to {@link ExtensionContext#getConfigurationParameter(String)}.</li>
|
||||
|
|
@ -296,17 +296,16 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
|||
* constructor. Consequently, no other registered {@link ParameterResolver}
|
||||
* will be able to resolve parameters.
|
||||
* @see #resolveParameter
|
||||
* @see TestConstructorUtils#isAutowirableConstructor(Constructor, Class)
|
||||
* @see TestConstructorUtils#isAutowirableConstructor(Executable, PropertyProvider)
|
||||
* @see ParameterResolutionDelegate#isAutowirable
|
||||
*/
|
||||
@Override
|
||||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
|
||||
Parameter parameter = parameterContext.getParameter();
|
||||
Executable executable = parameter.getDeclaringExecutable();
|
||||
Class<?> testClass = extensionContext.getRequiredTestClass();
|
||||
PropertyProvider junitPropertyProvider = propertyName ->
|
||||
extensionContext.getConfigurationParameter(propertyName).orElse(null);
|
||||
return (TestConstructorUtils.isAutowirableConstructor(executable, testClass, junitPropertyProvider) ||
|
||||
return (TestConstructorUtils.isAutowirableConstructor(executable, junitPropertyProvider) ||
|
||||
ApplicationContext.class.isAssignableFrom(parameter.getType()) ||
|
||||
supportsApplicationEvents(parameterContext) ||
|
||||
ParameterResolutionDelegate.isAutowirable(parameter, parameterContext.getIndex()));
|
||||
|
|
|
|||
|
|
@ -78,6 +78,7 @@ public abstract class TestConstructorUtils {
|
|||
private TestConstructorUtils() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the supplied executable for the given test class is an
|
||||
* autowirable constructor.
|
||||
|
|
@ -86,8 +87,11 @@ public abstract class TestConstructorUtils {
|
|||
* @param executable an executable for the test class
|
||||
* @param testClass the test class
|
||||
* @return {@code true} if the executable is an autowirable constructor
|
||||
* @see #isAutowirableConstructor(Executable, Class, PropertyProvider)
|
||||
* @see #isAutowirableConstructor(Executable, PropertyProvider)
|
||||
* @deprecated as of 6.2.13, in favor of {@link #isAutowirableConstructor(Executable, PropertyProvider)};
|
||||
* to be removed in Spring Framework 7.1
|
||||
*/
|
||||
@Deprecated(since = "6.2.13", forRemoval = true)
|
||||
public static boolean isAutowirableConstructor(Executable executable, Class<?> testClass) {
|
||||
return isAutowirableConstructor(executable, testClass, null);
|
||||
}
|
||||
|
|
@ -101,7 +105,10 @@ public abstract class TestConstructorUtils {
|
|||
* @param testClass the test class
|
||||
* @return {@code true} if the constructor is autowirable
|
||||
* @see #isAutowirableConstructor(Constructor, Class, PropertyProvider)
|
||||
* @deprecated as of 6.2.13, in favor of {@link #isAutowirableConstructor(Executable, PropertyProvider)};
|
||||
* to be removed in Spring Framework 7.1
|
||||
*/
|
||||
@Deprecated(since = "6.2.13", forRemoval = true)
|
||||
public static boolean isAutowirableConstructor(Constructor<?> constructor, Class<?> testClass) {
|
||||
return isAutowirableConstructor(constructor, testClass, null);
|
||||
}
|
||||
|
|
@ -119,7 +126,10 @@ public abstract class TestConstructorUtils {
|
|||
* @return {@code true} if the executable is an autowirable constructor
|
||||
* @since 5.3
|
||||
* @see #isAutowirableConstructor(Constructor, Class, PropertyProvider)
|
||||
* @deprecated as of 6.2.13, in favor of {@link #isAutowirableConstructor(Executable, PropertyProvider)};
|
||||
* to be removed in Spring Framework 7.1
|
||||
*/
|
||||
@Deprecated(since = "6.2.13", forRemoval = true)
|
||||
public static boolean isAutowirableConstructor(Executable executable, Class<?> testClass,
|
||||
@Nullable PropertyProvider fallbackPropertyProvider) {
|
||||
|
||||
|
|
@ -148,16 +158,62 @@ public abstract class TestConstructorUtils {
|
|||
* {@link TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME}).</li>
|
||||
* </ol>
|
||||
* @param constructor a constructor for the test class
|
||||
* @param testClass the test class
|
||||
* @param testClass the test class, typically the declaring class of the constructor
|
||||
* @param fallbackPropertyProvider fallback property provider used to look up
|
||||
* the value for the default <em>test constructor autowire mode</em> if no
|
||||
* such value is found in {@link SpringProperties}
|
||||
* the value for {@link TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME}
|
||||
* if no such value is found in {@link SpringProperties}; may be {@code null}
|
||||
* if there is no fallback support
|
||||
* @return {@code true} if the constructor is autowirable
|
||||
* @since 5.3
|
||||
* @see #isAutowirableConstructor(Executable, PropertyProvider)
|
||||
* @deprecated as of 6.2.13, in favor of {@link #isAutowirableConstructor(Executable, PropertyProvider)};
|
||||
* to be removed in Spring Framework 7.1
|
||||
*/
|
||||
@Deprecated(since = "6.2.13", forRemoval = true)
|
||||
public static boolean isAutowirableConstructor(Constructor<?> constructor, Class<?> testClass,
|
||||
@Nullable PropertyProvider fallbackPropertyProvider) {
|
||||
|
||||
return isAutowirableConstructorInternal(constructor, testClass, fallbackPropertyProvider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the supplied {@link Executable} is an autowirable {@link Constructor}.
|
||||
*
|
||||
* <p>A constructor is considered to be autowirable if one of the following
|
||||
* conditions is {@code true}.
|
||||
*
|
||||
* <ol>
|
||||
* <li>The constructor is annotated with {@link Autowired @Autowired},
|
||||
* {@link jakarta.inject.Inject @jakarta.inject.Inject}, or
|
||||
* {@link javax.inject.Inject @javax.inject.Inject}.</li>
|
||||
* <li>{@link TestConstructor @TestConstructor} is <em>present</em> or
|
||||
* <em>meta-present</em> on the test class with
|
||||
* {@link TestConstructor#autowireMode() autowireMode} set to
|
||||
* {@link AutowireMode#ALL ALL}.</li>
|
||||
* <li>The default <em>test constructor autowire mode</em> has been set to
|
||||
* {@code ALL} in {@link SpringProperties} or in the supplied fallback
|
||||
* {@link PropertyProvider}.</li>
|
||||
* </ol>
|
||||
* @param executable an {@code Executable} for a test class
|
||||
* @param fallbackPropertyProvider fallback property provider used to look up
|
||||
* the value for {@value TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME}
|
||||
* if no such value is found in {@link SpringProperties}; may be {@code null}
|
||||
* if there is no fallback support
|
||||
* @return {@code true} if the executable is an autowirable constructor
|
||||
* @since 6.2.13
|
||||
* @see TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME
|
||||
*/
|
||||
public static boolean isAutowirableConstructor(Executable executable,
|
||||
@Nullable PropertyProvider fallbackPropertyProvider) {
|
||||
|
||||
return (executable instanceof Constructor<?> constructor &&
|
||||
isAutowirableConstructorInternal(constructor, constructor.getDeclaringClass(), fallbackPropertyProvider));
|
||||
}
|
||||
|
||||
|
||||
private static boolean isAutowirableConstructorInternal(Constructor<?> constructor, Class<?> testClass,
|
||||
@Nullable PropertyProvider fallbackPropertyProvider) {
|
||||
|
||||
// Is the constructor annotated with @Autowired/@Inject?
|
||||
if (isAnnotatedWithAutowiredOrInject(constructor)) {
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -41,6 +41,9 @@ import static org.springframework.test.context.TestConstructor.AutowireMode.ANNO
|
|||
*/
|
||||
class TestConstructorUtilsTests {
|
||||
|
||||
private static final PropertyProvider propertyProvider = name -> null;
|
||||
|
||||
|
||||
@AfterEach
|
||||
void clearGlobalFlag() {
|
||||
setGlobalFlag(null);
|
||||
|
|
@ -100,12 +103,12 @@ class TestConstructorUtilsTests {
|
|||
|
||||
private void assertAutowirable(Class<?> testClass) throws NoSuchMethodException {
|
||||
Constructor<?> constructor = testClass.getDeclaredConstructor();
|
||||
assertThat(TestConstructorUtils.isAutowirableConstructor(constructor, testClass)).isTrue();
|
||||
assertThat(TestConstructorUtils.isAutowirableConstructor(constructor, propertyProvider)).isTrue();
|
||||
}
|
||||
|
||||
private void assertNotAutowirable(Class<?> testClass) throws NoSuchMethodException {
|
||||
Constructor<?> constructor = testClass.getDeclaredConstructor();
|
||||
assertThat(TestConstructorUtils.isAutowirableConstructor(constructor, testClass)).isFalse();
|
||||
assertThat(TestConstructorUtils.isAutowirableConstructor(constructor, propertyProvider)).isFalse();
|
||||
}
|
||||
|
||||
private void setGlobalFlag() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue