Search in sources :

Example 6 with MethodRule

use of org.junit.rules.MethodRule in project tomee by apache.

the class TomEEEmbeddedSingleRunner method rules.

@Override
protected List<MethodRule> rules(final Object test) {
    final List<MethodRule> rules = super.rules(test);
    rules.add(new MethodRule() {

        @Override
        public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
            return new Statement() {

                @Override
                public void evaluate() throws Throwable {
                    BASE.start(test);
                    BASE.composerInject(target);
                    base.evaluate();
                }
            };
        }
    });
    return rules;
}
Also used : MethodRule(org.junit.rules.MethodRule) Statement(org.junit.runners.model.Statement) FrameworkMethod(org.junit.runners.model.FrameworkMethod)

Example 7 with MethodRule

use of org.junit.rules.MethodRule in project junit4 by junit-team.

the class BlockJUnit4ClassRunner method withRules.

private Statement withRules(FrameworkMethod method, Object target, Statement statement) {
    RuleContainer ruleContainer = new RuleContainer();
    CURRENT_RULE_CONTAINER.set(ruleContainer);
    try {
        List<TestRule> testRules = getTestRules(target);
        for (MethodRule each : rules(target)) {
            if (!(each instanceof TestRule && testRules.contains(each))) {
                ruleContainer.add(each);
            }
        }
        for (TestRule rule : testRules) {
            ruleContainer.add(rule);
        }
    } finally {
        CURRENT_RULE_CONTAINER.remove();
    }
    return ruleContainer.apply(method, describeChild(method), target, statement);
}
Also used : TestRule(org.junit.rules.TestRule) MethodRule(org.junit.rules.MethodRule)

Example 8 with MethodRule

use of org.junit.rules.MethodRule in project junit4 by junit-team.

the class RuleContainer method getSortedEntries.

/**
 * Returns entries in the order how they should be applied, i.e. inner-to-outer.
 */
private List<RuleEntry> getSortedEntries() {
    List<RuleEntry> ruleEntries = new ArrayList<RuleEntry>(methodRules.size() + testRules.size());
    for (MethodRule rule : methodRules) {
        ruleEntries.add(new RuleEntry(rule, RuleEntry.TYPE_METHOD_RULE, orderValues.get(rule)));
    }
    for (TestRule rule : testRules) {
        ruleEntries.add(new RuleEntry(rule, RuleEntry.TYPE_TEST_RULE, orderValues.get(rule)));
    }
    Collections.sort(ruleEntries, ENTRY_COMPARATOR);
    return ruleEntries;
}
Also used : TestRule(org.junit.rules.TestRule) MethodRule(org.junit.rules.MethodRule) ArrayList(java.util.ArrayList)

Example 9 with MethodRule

use of org.junit.rules.MethodRule in project spock by spockframework.

the class MethodRuleInterceptor method intercept.

@Override
public void intercept(final IMethodInvocation invocation) throws Throwable {
    Statement statement = createBaseStatement(invocation);
    FrameworkMethod method = createFrameworkMethod(invocation);
    for (FieldInfo field : ruleFields) {
        MethodRule rule = (MethodRule) getRuleInstance(field, invocation.getInstance());
        statement = rule.apply(statement, method, invocation.getInstance());
    }
    statement.evaluate();
}
Also used : MethodRule(org.junit.rules.MethodRule) FieldInfo(org.spockframework.runtime.model.FieldInfo)

Aggregations

MethodRule (org.junit.rules.MethodRule)9 FrameworkMethod (org.junit.runners.model.FrameworkMethod)5 Statement (org.junit.runners.model.Statement)5 TestRule (org.junit.rules.TestRule)2 FieldInfo (org.spockframework.runtime.model.FieldInfo)2 HibernateTest (com.querydsl.jpa.HibernateTest)1 JPATest (com.querydsl.jpa.JPATest)1 ArrayList (java.util.ArrayList)1