Created
September 17, 2018 09:58
-
-
Save igorstojanovski/98b45348736676c94130ad22fb75707d to your computer and use it in GitHub Desktop.
Order of running rules in a rule chain in JUnit 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
========== THREE ============ | |
========== TWO ============ | |
========== ONE ============ | |
Evaluation ONE! | |
Evaluation TWO! | |
Evaluation THREE! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RulesTest { | |
@ClassRule | |
public static final TestRule CLASS_CHAIN = RuleChain.outerRule(new RuleOne()) | |
.around(new RuleTwo()) | |
.around(new RuleThree()); | |
@Test | |
public void shouldCallRulesInOrder() { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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