下面列出了怎么用org.springframework.test.context.TestConstructor的API类实例代码及写法,或者点击链接到github查看源代码。
@After
public void clearGlobalFlag() {
System.clearProperty(TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_PROPERTY_NAME);
}
private void setGlobalFlag() {
System.setProperty(TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_PROPERTY_NAME, "true");
}
/**
* 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);
}