Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
MethodRule TestRule ordering issue #383
Since the new TestRule interface some behaviours have change in the execution order of the @rules.
We have one rule which records Test Results. That Rule implements MethodsRule. Now in some Tests we ues ExpectedException Rule. But with a newer JUnit the ExpectedExceptino rule now is implemented via TestRule.
Because of the interface JUnit seems to group all @rule fields by there types, so reodering the fields in my Testscase doesn't change the overall order, only within the group. The code at the end should print the rule execution in the same order the fields are defined:
Method : rule04
but instead it's getting executed in a 'grouped' order :
TestRule : rule03
public class RuleOrder
I don't know if this is still a problem for you, but have you looked at the RuleChain?(https://github.com/KentBeck/junit/blob/master/src/main/java/org/junit/rules/RuleChain.java). Would this help? It doesn't accept MethodRules, but there is nothing to stop you from copying the code and modifying it to accept MethodRule and/or TestRule. This is now the way to define a rule execution order.
Yes this is still a issue.
You cannot fix this with a RuleChain because of issue #351 when rules need "this". The current workaround is to warp the TestMethod rules inside of a MethodRule. Not nice.
The Problem here is 3rd party rules that can change 'on the fly' and suddenly tests fail. ( That's how i stubled over this. Did a update of the internal testing depenency, which brought a new JUnit -> Bam Tests not working)
Therefore JUnit should be handling this correctly, everything else is a bad workaround.