public class RuleChain extends java.lang.Object implements TestRule
RuleChain
can be used for creating composite rules. You create a
RuleChain
with outerRule(TestRule)
and subsequent calls of
around(TestRule)
:
public abstract class CompositeRules { public static TestRule extendedLogging() { return RuleChain.outerRule(new LoggingRule("outer rule")) .around(new LoggingRule("middle rule")) .around(new LoggingRule("inner rule")); } }
public class UseRuleChain { @Rule public final TestRule extendedLogging = CompositeRules.extendedLogging(); @Test public void example() { assertTrue(true); } }writes the log
starting outer rule starting middle rule starting inner rule finished inner rule finished middle rule finished outer ruleIn older versions of JUnit (before 4.13)
RuleChain
was used for
ordering rules. We recommend to not use it for this purpose anymore. You can
use the attribute order
of the annotation Rule
or ClassRule
for ordering rules.Rule.order()
,
ClassRule.order()
Modifier and Type | Field and Description |
---|---|
private static RuleChain |
EMPTY_CHAIN |
private java.util.List<TestRule> |
rulesStartingWithInnerMost |
Modifier | Constructor and Description |
---|---|
private |
RuleChain(java.util.List<TestRule> rules) |
Modifier and Type | Method and Description |
---|---|
Statement |
apply(Statement base,
Description description)
Modifies the method-running
Statement to implement this
test-running rule. |
RuleChain |
around(TestRule enclosedRule)
|
static RuleChain |
emptyRuleChain()
Returns a
RuleChain without a TestRule . |
static RuleChain |
outerRule(TestRule outerRule)
Returns a
RuleChain with a single TestRule . |
private static final RuleChain EMPTY_CHAIN
private java.util.List<TestRule> rulesStartingWithInnerMost
private RuleChain(java.util.List<TestRule> rules)
public static RuleChain outerRule(TestRule outerRule)
outerRule
- the outer rule of the RuleChain
.RuleChain
with a single TestRule
.public RuleChain around(TestRule enclosedRule)
enclosedRule
- the rule to enclose; must not be null
.RuleChain
.java.lang.NullPointerException
- if the argument enclosedRule
is null
public Statement apply(Statement base, Description description)
Statement
to implement this
test-running rule.apply
in interface TestRule
base
- The Statement
to be modifieddescription
- A Description
of the test implemented in base
base
,
a wrapper around base
, or a completely new Statement.