Skip to content

Instantly share code, notes, and snippets.

@igorstojanovski
Created September 17, 2018 09:58
Show Gist options
  • Save igorstojanovski/98b45348736676c94130ad22fb75707d to your computer and use it in GitHub Desktop.
Save igorstojanovski/98b45348736676c94130ad22fb75707d to your computer and use it in GitHub Desktop.
Order of running rules in a rule chain in JUnit 4
========== THREE ============
========== TWO ============
========== ONE ============
Evaluation ONE!
Evaluation TWO!
Evaluation THREE!
public class RuleOne implements TestRule {
@Override
public Statement apply(Statement statement, Description description) {
System.out.println("========== ONE ============");
return new Statement() {
@Override
public void evaluate() throws Throwable {
System.out.println("Evaluation ONE!");
statement.evaluate();
}
};
}
}
public class RulesTest {
@ClassRule
public static final TestRule CLASS_CHAIN = RuleChain.outerRule(new RuleOne())
.around(new RuleTwo())
.around(new RuleThree());
@Test
public void shouldCallRulesInOrder() {
}
}
public class RuleThree implements TestRule {
@Override
public Statement apply(Statement statement, Description description) {
System.out.println("========== THREE ============");
return new Statement() {
@Override
public void evaluate() throws Throwable {
System.out.println("Evaluation THREE!");
statement.evaluate();
}
};
}
}
public class RuleTwo implements TestRule {
@Override
public Statement apply(Statement statement, Description description) {
System.out.println("========== TWO ============");
return new Statement() {
@Override
public void evaluate() throws Throwable {
System.out.println("Evaluation TWO!");
statement.evaluate();
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment