下面列出了org.junit.rules.ExternalResource#org.junit.ClassRule 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Throw an {@link IllegalStateException} if the supplied {@code testClass}
* does not declare a {@code public static final SpringClassRule} field
* that is annotated with {@code @ClassRule}.
*/
private static SpringClassRule validateSpringClassRuleConfiguration(Class<?> testClass) {
Field ruleField = null;
for (Field field : testClass.getFields()) {
if (ReflectionUtils.isPublicStaticFinal(field) && SpringClassRule.class.isAssignableFrom(field.getType())) {
ruleField = field;
break;
}
}
if (ruleField == null) {
throw new IllegalStateException(String.format(
"Failed to find 'public static final SpringClassRule' field in test class [%s]. " +
"Consult the javadoc for SpringClassRule for details.", testClass.getName()));
}
if (!ruleField.isAnnotationPresent(ClassRule.class)) {
throw new IllegalStateException(String.format(
"SpringClassRule field [%s] must be annotated with JUnit's @ClassRule annotation. " +
"Consult the javadoc for SpringClassRule for details.", ruleField));
}
return (SpringClassRule) ReflectionUtils.getField(ruleField, null);
}
@Override
public void testStarted(Description description) throws Exception {
Category[] categories = description.getTestClass().getAnnotationsByType(Category.class);
// @Category is not repeatable -- it is only possible to get an array of length zero or one.
if (categories.length == 1) {
for (Class<?> c : categories[0].value()) {
if (c == IntegrationTests.class) {
return;
}
}
}
for (Field field : description.getTestClass().getFields()) {
if (Modifier.isStatic(field.getModifiers()) && field.getType() == HBaseClassTestRule.class &&
field.isAnnotationPresent(ClassRule.class)) {
HBaseClassTestRule timeout = (HBaseClassTestRule) field.get(null);
assertEquals(
"The HBaseClassTestRule ClassRule in " + description.getTestClass().getName() +
" is for " + timeout.getClazz().getName(),
description.getTestClass(), timeout.getClazz());
return;
}
}
fail("No HBaseClassTestRule ClassRule for " + description.getTestClass().getName());
}
@ClassRule
public static final ConsulResource consul(){
int port = SocketUtils.findAvailableTcpPort();
ConsulResource consulResource = new ConsulResource(port);
System.setProperty("spring.cloud.consul.port",String.valueOf(port));
return consulResource;
}
/**
* Validates the annotation of the rule field in the test class.
*
* @param description
*/
private void validateRuleAnnotations(Description description) {
// If the first run is a @ClassRule run, check if @Rule is annotated
if (firstRun && !description.isTest()) {
/*
* Get the fields of the test class and check if there is only one
* coverage rule and if the coverage rule field is annotation with
* both @ClassRule and @Rule.
*/
int numberOfCoverageRules = 0;
for (Field field : description.getTestClass().getFields()) {
final Class<?> fieldType = field.getType();
if (getClass().isAssignableFrom(fieldType)) {
++numberOfCoverageRules;
final boolean isClassRule = field.isAnnotationPresent(ClassRule.class);
final boolean isRule = field.isAnnotationPresent(Rule.class);
if (isClassRule && !isRule) {
throw new RuntimeException(getClass().getCanonicalName()
+ " can only be used as a @ClassRule if it is also a @Rule!");
}
}
}
// TODO if they really want to have multiple runs, let them?
if (numberOfCoverageRules > 1) {
throw new RuntimeException("Only one coverage rule can be used per test class!");
}
}
}
/**
* @return the {@code ClassRule}s that can transform the block that runs
* each method in the tested class.
*/
protected List<TestRule> classRules() {
List<TestRule> result = getTestClass().getAnnotatedMethodValues(null, ClassRule.class, TestRule.class);
result.addAll(getTestClass().getAnnotatedFieldValues(null, ClassRule.class, TestRule.class));
return result;
}
private List<TestRule> getClassRules() {
return Stream.concat(
testClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class).stream(),
testClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class).stream())
.collect(Collectors.toList());
}
@ClassRule
public static TestRule rule() {
return new ExternalResource() {
};
}
/**
* Creates a class test rule.
*
* @return a class test rule.
*/
@ClassRule
public static TestRule classRule() {
return new ClassPathRule(AbstractTestSpringConfigurationFileAutoDetectingTest.class);
}
/**
* Creates a class test rule.
*
* @return a class test rule.
*/
@ClassRule
public static TestRule classRule() {
return new ClassPathRule(AbstractTestConfigurationFileInvalidTest.class);
}
/**
* Creates a class test rule.
*
* @return a class test rule.
*/
@ClassRule
public static TestRule classRule() {
return new ClassPathRule(AbstractMainSpringConfigurationFileAutoDetectingTest.class);
}
/**
* Creates a class test rule.
*
* @return a class test rule.
*/
@ClassRule
public static TestRule classRule() {
return new ClassPathRule(AbstractTestSpringConfigurationFileInvalidTest.class);
}
/**
* Creates a class test rule.
*
* @return a class test rule.
*/
@ClassRule
public static TestRule classRule() {
return new ClassPathRule(AbstractTestConfigurationFileAutoDetectingTest.class);
}
/**
* Creates a class test rule.
*
* @return a class test rule.
*/
@ClassRule
public static TestRule classRule() {
return new ClassPathRule(AbstractMainConfigurationFileAutoDetectingTest.class);
}
/**
* Creates a class test rule.
*
* @return a class test rule.
*/
@ClassRule
public static TestRule classRule() {
return new ClassPathRule(AbstractMainConfigurationFileInvalidTest.class);
}
/**
* Creates a class test rule.
*
* @return a class test rule.
*/
@ClassRule
public static TestRule classRule() {
return new ClassPathRule(AbstractMainSpringConfigurationFileInvalidTest.class);
}