Package org.junit.rules
Class RuleChain
java.lang.Object
org.junit.rules.RuleChain
- All Implemented Interfaces:
TestRule
The
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.- Since:
- 4.10
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
-
Field Details
-
EMPTY_CHAIN
-
rulesStartingWithInnerMost
-
-
Constructor Details
-
RuleChain
-
-
Method Details
-
emptyRuleChain
- Returns:
- a
RuleChainwithout aTestRule.
-
outerRule
- Parameters:
outerRule- the outer rule of theRuleChain.- Returns:
- a
RuleChainwith a singleTestRule.
-
around
- Parameters:
enclosedRule- the rule to enclose; must not benull.- Returns:
- a new
RuleChain. - Throws:
NullPointerException- if the argumentenclosedRuleisnull
-
apply
Modifies the method-runningStatementto implement this test-running rule.- Specified by:
applyin interfaceTestRule- Parameters:
base- TheStatementto be modifieddescription- ADescriptionof the test implemented inbase- Returns:
- a new statement, which may be the same as
base, a wrapper aroundbase, or a completely new Statement.
-