use of org.springframework.test.context.TestConstructor in project spring-framework by spring-projects.
the class TestConstructorUtils method isAutowirableConstructor.
/**
* Determine if the supplied constructor for the given test class is
* autowirable.
*
* <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}.</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} (see
* {@link TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME}).</li>
* </ol>
* @param constructor a constructor for the test class
* @param testClass the test class
* @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}
* @return {@code true} if the constructor is autowirable
* @since 5.3
*/
public static boolean isAutowirableConstructor(Constructor<?> constructor, Class<?> testClass, @Nullable PropertyProvider fallbackPropertyProvider) {
// Is the constructor annotated with @Autowired?
if (AnnotatedElementUtils.hasAnnotation(constructor, Autowired.class)) {
return true;
}
AutowireMode autowireMode = null;
// Is the test class annotated with @TestConstructor?
TestConstructor testConstructor = TestContextAnnotationUtils.findMergedAnnotation(testClass, TestConstructor.class);
if (testConstructor != null) {
autowireMode = testConstructor.autowireMode();
} else {
// Custom global default from SpringProperties?
String value = SpringProperties.getProperty(TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME);
autowireMode = AutowireMode.from(value);
// Use fallback provider?
if (autowireMode == null && fallbackPropertyProvider != null) {
value = fallbackPropertyProvider.get(TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME);
autowireMode = AutowireMode.from(value);
}
}
return (autowireMode == AutowireMode.ALL);
}
use of org.springframework.test.context.TestConstructor in project spring-framework by spring-projects.
the class SpringExtension method supportsParameter.
/**
* Determine if the value for the {@link Parameter} in the supplied {@link ParameterContext}
* should be autowired from the test's {@link ApplicationContext}.
* <p>A parameter is considered to be autowirable if one of the following
* conditions is {@code true}.
* <ol>
* <li>The {@linkplain ParameterContext#getDeclaringExecutable() declaring
* executable} is a {@link Constructor} and
* {@link TestConstructorUtils#isAutowirableConstructor(Constructor, Class, 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>
* <li>The parameter is of type {@link ApplicationContext} or a sub-type thereof.</li>
* <li>The parameter is of type {@link ApplicationEvents} or a sub-type thereof.</li>
* <li>{@link ParameterResolutionDelegate#isAutowirable} returns {@code true}.</li>
* </ol>
* <p><strong>WARNING</strong>: If a test class {@code Constructor} is annotated
* with {@code @Autowired} or automatically autowirable (see {@link TestConstructor}),
* Spring will assume the responsibility for resolving all parameters in the
* constructor. Consequently, no other registered {@link ParameterResolver}
* will be able to resolve parameters.
* @see #resolveParameter
* @see TestConstructorUtils#isAutowirableConstructor(Constructor, Class)
* @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) || ApplicationContext.class.isAssignableFrom(parameter.getType()) || supportsApplicationEvents(parameterContext) || ParameterResolutionDelegate.isAutowirable(parameter, parameterContext.getIndex()));
}
Aggregations