Search in sources :

Example 1 with AutowireMode

use of org.springframework.test.context.TestConstructor.AutowireMode 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);
}
Also used : AutowireMode(org.springframework.test.context.TestConstructor.AutowireMode) TestConstructor(org.springframework.test.context.TestConstructor)

Aggregations

TestConstructor (org.springframework.test.context.TestConstructor)1 AutowireMode (org.springframework.test.context.TestConstructor.AutowireMode)1