Package org.junit.rules
Class RuleChain
- java.lang.Object
- org.junit.rules.RuleChain
- All Implemented Interfaces:
TestRule
public class RuleChain extends Object implements TestRule
The RuleChain rule allows ordering of TestRules. You create aRuleChain
withouterRule(TestRule)
and subsequent calls ofaround(TestRule)
:public static class UseRuleChain { @Rule public RuleChain chain= RuleChain .outerRule(new LoggingRule("outer rule") .around(new LoggingRule("middle rule") .around(new LoggingRule("inner rule"); @Test public void example() { assertTrue(true); } }
writes the logstarting outer rule starting middle rule starting inner rule finished inner rule finished middle rule finished outer rule
- Since:
- 4.10
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Statement
apply(Statement base, Description description)
Modifies the method-runningStatement
to implement this test-running rule.RuleChain
around(TestRule enclosedRule)
Create a newRuleChain
, which encloses thenextRule
with the rules of the currentRuleChain
.static RuleChain
emptyRuleChain()
Returns aRuleChain
without aTestRule
.static RuleChain
outerRule(TestRule outerRule)
Returns aRuleChain
with a singleTestRule
.
Method Detail
emptyRuleChain
public static RuleChain emptyRuleChain()
- Returns:
- a
RuleChain
without aTestRule
.
outerRule
public static RuleChain outerRule(TestRule outerRule)
- Parameters:
outerRule
- the outer rule of theRuleChain
.- Returns:
- a
RuleChain
with a singleTestRule
.
around
public RuleChain around(TestRule enclosedRule)
Create a newRuleChain
, which encloses thenextRule
with the rules of the currentRuleChain
.- Parameters:
enclosedRule
- the rule to enclose.- Returns:
- a new
RuleChain
.
apply
public Statement apply(Statement base, Description description)
Modifies the method-runningStatement
to implement this test-running rule.- Specified by:
apply
in interfaceTestRule
- Parameters:
base
- TheStatement
to be modifieddescription
- ADescription
of the test implemented inbase
- Returns:
- a new statement, which may be the same as
base
, a wrapper aroundbase
, or a completely new Statement.