org.springframework.test.context.TestConstructor Java Examples

The following examples show how to use org.springframework.test.context.TestConstructor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestConstructorUtilsTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@After
public void clearGlobalFlag() {
	System.clearProperty(TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_PROPERTY_NAME);
}
 
Example #2
Source File: TestConstructorUtilsTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void setGlobalFlag() {
	System.setProperty(TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_PROPERTY_NAME, "true");
}
 
Example #3
Source File: TestConstructorUtils.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * 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#autowire autowire} set to {@code true}.</li>
 * <li>The default <em>test constructor autowire</em> mode is set to {@code true}
 * (see {@link TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_PROPERTY_NAME}).</li>
 * </ol>
 *
 * @param constructor a constructor for the test class
 * @param testClass the test class
 * @return {@code true} if the constructor is autowirable
 * @see #isAutowirableConstructor(Executable, Class)
 */
public static boolean isAutowirableConstructor(Constructor<?> constructor, Class<?> testClass) {
	// Is the constructor annotated with @Autowired?
	if (AnnotatedElementUtils.hasAnnotation(constructor, Autowired.class)) {
		return true;
	}
	// Is the test class annotated with @TestConstructor?
	TestConstructor testConstructor = AnnotatedElementUtils.findMergedAnnotation(testClass, TestConstructor.class);
	if (testConstructor != null) {
		return testConstructor.autowire();
	}
	// Else use global default.
	return SpringProperties.getFlag(TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_PROPERTY_NAME);
}