Package org.junit.rules
Class RuleChain
- java.lang.Object
-
- org.junit.rules.RuleChain
-
- All Implemented Interfaces:
TestRule
public class RuleChain extends java.lang.Object implements TestRule
TheRuleChaincan be used for creating composite rules. You create aRuleChainwithouterRule(TestRule)and subsequent calls ofaround(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 logstarting outer rule starting middle rule starting inner rule finished inner rule finished middle rule finished outer rule
In older versions of JUnit (before 4.13)RuleChainwas used for ordering rules. We recommend to not use it for this purpose anymore. You can use the attributeorderof the annotationRuleorClassRulefor ordering rules.- Since:
- 4.10
- See Also:
Rule.order(),ClassRule.order()
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Statementapply(Statement base, Description description)Modifies the method-runningStatementto implement this test-running rule.RuleChainaround(TestRule enclosedRule)static RuleChainemptyRuleChain()Returns aRuleChainwithout aTestRule.static RuleChainouterRule(TestRule outerRule)Returns aRuleChainwith a singleTestRule.
-
-
-
Method Detail
-
emptyRuleChain
public static RuleChain emptyRuleChain()
- Returns:
- a
RuleChainwithout aTestRule.
-
outerRule
public static RuleChain outerRule(TestRule outerRule)
- Parameters:
outerRule- the outer rule of theRuleChain.- Returns:
- a
RuleChainwith a singleTestRule.
-
around
public RuleChain around(TestRule enclosedRule)
- Parameters:
enclosedRule- the rule to enclose; must not benull.- Returns:
- a new
RuleChain. - Throws:
java.lang.NullPointerException- if the argumentenclosedRuleisnull
-
apply
public Statement apply(Statement base, Description description)
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.
-
-