Skip to content

MethodRule TestRule ordering issue #383

Closed as not planned
Closed as not planned
@RainerW

Description

@RainerW

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions