What is the equivalent of @RuleChain in JUnit 5?
Asked Answered
N

3

10
  • I have 2 "class level" rules : MyRule1 and MyRule2
  • MyRule2 depends on MyRule1
  • MyRule1 "before" method should therefore run before the MyRule2 "before" method.

In JUnit 4, it can be implemented this way, via the RuleChain :

static MyRule1 myRule1 = new MyRule1();
static MyRule2 myRule2 = new MyRule2(myRule1);

@Rule
TestRule ruleChain = RuleChain.outerRule(myRule1)
        .around(myRule2);

In JUnit 5, I have to implement it this way :

static MyRule1 myRule1 = new MyRule1();

@RegisterExtension
static MyRule2 myRule2 = new MyRule2(myRule1);

with MyRule2 :

class MyRule2 implements BeforeAllCallback {

    private final MyRule1 myRule1;

    public MyRule2(MyRule1 myRule1) {
        this.myRule1 = myRule1;
    }

    @Override
    public void beforeAll(ExtensionContext extensionContext) {
        this.myRule1.beforeAll();

        X x = this.myRule1.getX();
        // do Rule 2 stuff with x
    }
}

It's the equivalent of the JUnit 4 implementation when it comes to the result.

But I have to explicitly and manually call the beforeAll() callback of MyRule1 in MyRule2.

I would like that MyRule2 would not be responsible for MyRule1 execution.

I went through the Extension Model documentation of JUnit 5 but didn't find anything on extensions that depend on other extensions.

Napery answered 4/10, 2018 at 12:40 Comment(0)
O
1

For extensions registered via @RegisterExtension, there is currently (as of JUnit Jupiter 5.3.1) no built-in support analogous to JUnit 4's RuleChain.

However, this issue links to a custom solution and also proposes support for @Order to control the order in which extensions are executed.

Osana answered 26/10, 2018 at 12:1 Comment(0)
C
2

To quote Jupiter's documentation:

Extensions registered declaratively via @ExtendWith will be executed in the order in which they are declared in the source code.

So, in your case, you should just declare them in that order:

@ExtendsWith({Rule1.class, Rule2.class})
public class MyTest {
Cupriferous answered 4/10, 2018 at 14:0 Comment(2)
Sorry, I did not explain that I needed to access MyRule1 to do something in MyRule2. I updated my question. However if I do what you're suggesting, will JUnit5 inject Rule1 into Rule 2 ? I doubt it.Napery
I'd be curious to know the answer to that too, because we have a similar situation where some of the rules are dependent on the other rules. (Most commonly, TempFolder.)Flute
O
1

For extensions registered via @RegisterExtension, there is currently (as of JUnit Jupiter 5.3.1) no built-in support analogous to JUnit 4's RuleChain.

However, this issue links to a custom solution and also proposes support for @Order to control the order in which extensions are executed.

Osana answered 26/10, 2018 at 12:1 Comment(0)
T
0

For extensions registered via @RegisterExtension, some libraries as testy-box provide a support for Chained Extensions.

Thames answered 24/12, 2022 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.