Package org.junit.rules
Interface TestRule
-
- All Known Implementing Classes:
DisableOnDebug,ErrorCollector,ExpectedException,ExternalResource,RuleChain,Stopwatch,Stopwatch.InternalWatcher,TemporaryFolder,TestName,TestWatcher,Timeout,Verifier
public interface TestRuleA TestRule is an alteration in how a test method, or set of test methods, is run and reported. ATestRulemay add additional checks that cause a test that would otherwise fail to pass, or it may perform necessary setup or cleanup for tests, or it may observe test execution to report it elsewhere.TestRules can do everything that could be done previously with methods annotated withBefore,After,BeforeClass, orAfterClass, but they are more powerful, and more easily shared between projects and classes. The default JUnit test runners for suites and individual test cases recognizeTestRules introduced in two different ways.Ruleannotates method-levelTestRules, andClassRuleannotates class-levelTestRules. See Javadoc for those annotations for more information. MultipleTestRules can be applied to a test or suite execution. TheStatementthat executes the method or suite is passed to each annotatedRulein turn, and each may return a substitute or modifiedStatement, which is passed to the nextRule, if any. For examples of how this can be useful, see these provided TestRules, or write your own:ErrorCollector: collect multiple errors in one test methodExpectedException: make flexible assertions about thrown exceptionsExternalResource: start and stop a server, for exampleTemporaryFolder: create fresh files, and delete after testTestName: remember the test name for use during the methodTestWatcher: add logic at events during method executionTimeout: cause test to fail after a set timeVerifier: fail test if object state ends up incorrect
- Since:
- 4.9
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Statementapply(Statement base, Description description)Modifies the method-runningStatementto implement this test-running rule.
-
-
-
Method Detail
-
apply
Statement apply(Statement base, Description description)
Modifies the method-runningStatementto implement this test-running rule.- 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.
-
-