Browse Source

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
pull/35687/head
Sam Brannen 2 months ago
parent
commit
82c34f7b51
  1. 7
      spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java
  2. 64
      spring-test/src/main/java/org/springframework/test/context/support/TestConstructorUtils.java
  3. 7
      spring-test/src/test/java/org/springframework/test/context/support/TestConstructorUtilsTests.java

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

@ -282,7 +282,7 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
* <ol> * <ol>
* <li>The {@linkplain ParameterContext#getDeclaringExecutable() declaring * <li>The {@linkplain ParameterContext#getDeclaringExecutable() declaring
* executable} is a {@link Constructor} and * 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 * returns {@code true}. Note that {@code isAutowirableConstructor()} will be
* invoked with a fallback {@link PropertyProvider} that delegates its lookup * invoked with a fallback {@link PropertyProvider} that delegates its lookup
* to {@link ExtensionContext#getConfigurationParameter(String)}.</li> * to {@link ExtensionContext#getConfigurationParameter(String)}.</li>
@ -296,17 +296,16 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
* constructor. Consequently, no other registered {@link ParameterResolver} * constructor. Consequently, no other registered {@link ParameterResolver}
* will be able to resolve parameters. * will be able to resolve parameters.
* @see #resolveParameter * @see #resolveParameter
* @see TestConstructorUtils#isAutowirableConstructor(Constructor, Class) * @see TestConstructorUtils#isAutowirableConstructor(Executable, PropertyProvider)
* @see ParameterResolutionDelegate#isAutowirable * @see ParameterResolutionDelegate#isAutowirable
*/ */
@Override @Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
Parameter parameter = parameterContext.getParameter(); Parameter parameter = parameterContext.getParameter();
Executable executable = parameter.getDeclaringExecutable(); Executable executable = parameter.getDeclaringExecutable();
Class<?> testClass = extensionContext.getRequiredTestClass();
PropertyProvider junitPropertyProvider = propertyName -> PropertyProvider junitPropertyProvider = propertyName ->
extensionContext.getConfigurationParameter(propertyName).orElse(null); extensionContext.getConfigurationParameter(propertyName).orElse(null);
return (TestConstructorUtils.isAutowirableConstructor(executable, testClass, junitPropertyProvider) || return (TestConstructorUtils.isAutowirableConstructor(executable, junitPropertyProvider) ||
ApplicationContext.class.isAssignableFrom(parameter.getType()) || ApplicationContext.class.isAssignableFrom(parameter.getType()) ||
supportsApplicationEvents(parameterContext) || supportsApplicationEvents(parameterContext) ||
ParameterResolutionDelegate.isAutowirable(parameter, parameterContext.getIndex())); ParameterResolutionDelegate.isAutowirable(parameter, parameterContext.getIndex()));

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

@ -78,6 +78,7 @@ public abstract class TestConstructorUtils {
private TestConstructorUtils() { private TestConstructorUtils() {
} }
/** /**
* Determine if the supplied executable for the given test class is an * Determine if the supplied executable for the given test class is an
* autowirable constructor. * autowirable constructor.
@ -86,8 +87,11 @@ public abstract class TestConstructorUtils {
* @param executable an executable for the test class * @param executable an executable for the test class
* @param testClass the test class * @param testClass the test class
* @return {@code true} if the executable is an autowirable constructor * @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) { public static boolean isAutowirableConstructor(Executable executable, Class<?> testClass) {
return isAutowirableConstructor(executable, testClass, null); return isAutowirableConstructor(executable, testClass, null);
} }
@ -101,7 +105,10 @@ public abstract class TestConstructorUtils {
* @param testClass the test class * @param testClass the test class
* @return {@code true} if the constructor is autowirable * @return {@code true} if the constructor is autowirable
* @see #isAutowirableConstructor(Constructor, Class, PropertyProvider) * @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) { public static boolean isAutowirableConstructor(Constructor<?> constructor, Class<?> testClass) {
return isAutowirableConstructor(constructor, testClass, null); return isAutowirableConstructor(constructor, testClass, null);
} }
@ -119,7 +126,10 @@ public abstract class TestConstructorUtils {
* @return {@code true} if the executable is an autowirable constructor * @return {@code true} if the executable is an autowirable constructor
* @since 5.3 * @since 5.3
* @see #isAutowirableConstructor(Constructor, Class, PropertyProvider) * @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, public static boolean isAutowirableConstructor(Executable executable, Class<?> testClass,
@Nullable PropertyProvider fallbackPropertyProvider) { @Nullable PropertyProvider fallbackPropertyProvider) {
@ -148,16 +158,62 @@ public abstract class TestConstructorUtils {
* {@link TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME}).</li> * {@link TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME}).</li>
* </ol> * </ol>
* @param constructor a constructor for the test class * @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 * @param fallbackPropertyProvider fallback property provider used to look up
* the value for the default <em>test constructor autowire mode</em> if no * the value for {@link TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME}
* such value is found in {@link SpringProperties} * 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 * @return {@code true} if the constructor is autowirable
* @since 5.3 * @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, public static boolean isAutowirableConstructor(Constructor<?> constructor, Class<?> testClass,
@Nullable PropertyProvider fallbackPropertyProvider) { @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? // Is the constructor annotated with @Autowired/@Inject?
if (isAnnotatedWithAutowiredOrInject(constructor)) { if (isAnnotatedWithAutowiredOrInject(constructor)) {
return true; return true;

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

@ -41,6 +41,9 @@ import static org.springframework.test.context.TestConstructor.AutowireMode.ANNO
*/ */
class TestConstructorUtilsTests { class TestConstructorUtilsTests {
private static final PropertyProvider propertyProvider = name -> null;
@AfterEach @AfterEach
void clearGlobalFlag() { void clearGlobalFlag() {
setGlobalFlag(null); setGlobalFlag(null);
@ -100,12 +103,12 @@ class TestConstructorUtilsTests {
private void assertAutowirable(Class<?> testClass) throws NoSuchMethodException { private void assertAutowirable(Class<?> testClass) throws NoSuchMethodException {
Constructor<?> constructor = testClass.getDeclaredConstructor(); Constructor<?> constructor = testClass.getDeclaredConstructor();
assertThat(TestConstructorUtils.isAutowirableConstructor(constructor, testClass)).isTrue(); assertThat(TestConstructorUtils.isAutowirableConstructor(constructor, propertyProvider)).isTrue();
} }
private void assertNotAutowirable(Class<?> testClass) throws NoSuchMethodException { private void assertNotAutowirable(Class<?> testClass) throws NoSuchMethodException {
Constructor<?> constructor = testClass.getDeclaredConstructor(); Constructor<?> constructor = testClass.getDeclaredConstructor();
assertThat(TestConstructorUtils.isAutowirableConstructor(constructor, testClass)).isFalse(); assertThat(TestConstructorUtils.isAutowirableConstructor(constructor, propertyProvider)).isFalse();
} }
private void setGlobalFlag() { private void setGlobalFlag() {

Loading…
Cancel
Save