use of org.junit.rules.TestRule in project pact-jvm by DiUS.
the class InteractionRunner method withRules.
protected Statement withRules(final Interaction interaction, final Object target, final Statement statement) {
final List<TestRule> testRules = testClass.getAnnotatedMethodValues(target, Rule.class, TestRule.class);
testRules.addAll(testClass.getAnnotatedFieldValues(target, Rule.class, TestRule.class));
return testRules.isEmpty() ? statement : new RunRules(statement, testRules, describeChild(interaction));
}
use of org.junit.rules.TestRule in project hazelcast by hazelcast.
the class AbstractHazelcastClassRunner method getTestRules.
@Override
protected List<TestRule> getTestRules(Object target) {
List<TestRule> testRules = super.getTestRules(target);
Set<Class<? extends TestRule>> testRuleClasses = new HashSet<Class<? extends TestRule>>();
TestClass testClass = getTestClass();
// find the required test rule classes from test class itself
Annotation[] classAnnotations = testClass.getAnnotations();
for (Annotation annotation : classAnnotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
AutoRegisteredTestRule autoFilterRule = annotationType.getAnnotation(AutoRegisteredTestRule.class);
if (autoFilterRule != null) {
Class<? extends TestRule> testRuleClass = autoFilterRule.testRule();
testRuleClasses.add(testRuleClass);
}
}
// find the required test rule classes from methods
List<FrameworkMethod> annotatedMethods = testClass.getAnnotatedMethods();
for (FrameworkMethod annotatedMethod : annotatedMethods) {
Annotation[] methodAnnotations = annotatedMethod.getAnnotations();
for (Annotation annotation : methodAnnotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
AutoRegisteredTestRule autoFilterRule = annotationType.getAnnotation(AutoRegisteredTestRule.class);
if (autoFilterRule != null) {
Class<? extends TestRule> testRuleClass = autoFilterRule.testRule();
testRuleClasses.add(testRuleClass);
}
}
}
// create and register test rules
for (Class<? extends TestRule> testRuleClass : testRuleClasses) {
try {
TestRule testRule = testRuleClass.newInstance();
testRules.add(testRule);
} catch (Throwable t) {
System.err.println("Unable to create test rule instance of test rule class " + testRuleClass.getName() + " because of " + t);
}
}
return testRules;
}
use of org.junit.rules.TestRule in project hazelcast by hazelcast.
the class AbstractHazelcastClassRunner method withBefores.
// Override withBefores to accommodate spawning the member bouncing thread after @Before's have been executed and before @Test
// when MemberBounceRule is in use
@Override
protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) {
List<FrameworkMethod> befores = getTestClass().getAnnotatedMethods(Before.class);
List<TestRule> testRules = getTestRules(target);
Statement nextStatement = statement;
if (!testRules.isEmpty()) {
for (TestRule rule : testRules) {
if (rule instanceof BounceMemberRule) {
nextStatement = ((BounceMemberRule) rule).startBouncing(statement);
}
}
}
return befores.isEmpty() ? nextStatement : new RunBefores(nextStatement, befores, target);
}
use of org.junit.rules.TestRule in project hazelcast by hazelcast.
the class AbstractHazelcastClassRunner method withAfters.
@Override
protected Statement withAfters(FrameworkMethod method, Object target, Statement statement) {
List<FrameworkMethod> afters = getTestClass().getAnnotatedMethods(After.class);
Statement nextStatement = statement;
List<TestRule> testRules = getTestRules(target);
if (!testRules.isEmpty()) {
for (TestRule rule : testRules) {
if (rule instanceof BounceMemberRule) {
nextStatement = ((BounceMemberRule) rule).stopBouncing(statement);
}
}
}
if (THREAD_DUMP_ON_FAILURE) {
return new ThreadDumpAwareRunAfters(method, nextStatement, afters, target);
}
if (afters.isEmpty()) {
return nextStatement;
} else {
return new RunAfters(nextStatement, afters, target);
}
}
use of org.junit.rules.TestRule in project randomizedtesting by randomizedtesting.
the class RandomizedRunner method wrapMethodRules.
/**
* Wrap the given statement in any declared MethodRules (old style rules).
*/
@SuppressWarnings("deprecation")
private Statement wrapMethodRules(Statement s, TestCandidate c, Object instance) {
FrameworkMethod fm = new FrameworkMethod(c.method);
// Old-style MethodRules first.
List<org.junit.rules.MethodRule> methodRules = getAnnotatedFieldValues(instance, Rule.class, org.junit.rules.MethodRule.class);
for (org.junit.rules.MethodRule rule : methodRules) {
s = rule.apply(s, fm, instance);
}
// New-style TestRule next.
List<TestRule> testRules = getAnnotatedFieldValues(instance, Rule.class, TestRule.class);
for (TestRule rule : testRules) {
s = rule.apply(s, c.description);
}
return s;
}
Aggregations