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

Open
RainerW opened this Issue Feb 15, 2012 · 4 comments

Comments

Projects
None yet
4 participants
@RainerW

RainerW commented Feb 15, 2012

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();
}
};
}
};
}

@matthewfarwell

This comment has been minimized.

Show comment
Hide comment
@matthewfarwell

matthewfarwell Apr 10, 2012

Contributor

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.

Contributor

matthewfarwell commented Apr 10, 2012

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.

@RainerW

This comment has been minimized.

Show comment
Hide comment
@RainerW

RainerW Apr 11, 2012

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.

RainerW commented Apr 11, 2012

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.

@dsaff

This comment has been minimized.

Show comment
Hide comment
@dsaff

dsaff Oct 11, 2013

Member

For those following along at home, the hope is to open with the proposal at #351 (comment), and then make these issues go away.

Member

dsaff commented Oct 11, 2013

For those following along at home, the hope is to open with the proposal at #351 (comment), and then make these issues go away.

@panchenko

This comment has been minimized.

Show comment
Hide comment
@panchenko

panchenko May 14, 2017

Contributor

The ordering of rules is addressed in #1445

Contributor

panchenko commented May 14, 2017

The ordering of rules is addressed in #1445

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment