-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MethodRule TestRule ordering issue #383
Comments
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. |
For those following along at home, the hope is to open with the proposal at #351 (comment), and then make these issues go away. |
The ordering of rules is addressed in #1445 |
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:
EXPECTED:
Method : rule04
TestRule : rule03
TestRule : rule02
Method : rule01
The test
but instead it's getting executed in a 'grouped' order :
ACTUAL:
TestRule : rule03
TestRule : rule02
Method : rule04
Method : rule01
The test
CODE:
public class RuleOrder
{
@rule
public MethodRuleImpl rule01 = new MethodRuleImpl("rule01");
@rule
public TestRuleImpl rule02 = new TestRuleImpl("rule02");
@rule
public TestRuleImpl rule03 = new TestRuleImpl("rule03");
@rule
public MethodRuleImpl rule04 = new MethodRuleImpl("rule04");
@test
public void aTest() {
System.out.println("The test");
}
class TestRuleImpl implements TestRule {
String text;
public TestRuleImpl(String text) { this.text = text; }
@OverRide
public Statement apply(final Statement base, Description description) {
return new Statement() {
public void evaluate() throws Throwable {
System.out.println("TestRule : " + text);
base.evaluate();
}
};
}
};
class MethodRuleImpl implements MethodRule {
String text;
public MethodRuleImpl(String text) { this.text = text; }
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
return new Statement() {
@OverRide
public void evaluate() throws Throwable
{
System.out.println("Method : " + text);
base.evaluate();
}
};
}
};
}
The text was updated successfully, but these errors were encountered: