Package org.junit.rules
Class TestWatcher
- java.lang.Object
-
- org.junit.rules.TestWatcher
-
- All Implemented Interfaces:
TestRule
- Direct Known Subclasses:
Stopwatch.InternalWatcher,TestName
public abstract class TestWatcher extends java.lang.Object implements TestRule
TestWatcher is a base class for Rules that take note of the testing action, without modifying it. For example, this class will keep a log of each passing and failing test:public static class WatchmanTest { private static String watchedLog; @Rule(order = Integer.MIN_VALUE) public TestWatcher watchman= new TestWatcher() { @Override protected void failed(Throwable e, Description description) { watchedLog+= description + "\n"; } @Override protected void succeeded(Description description) { watchedLog+= description + " " + "success!\n"; } }; @Test public void fails() { fail(); } @Test public void succeeds() { } }It is recommended to always set the
orderof theTestWatchertoInteger.MIN_VALUEso that it encloses all other rules. Otherwise it may see failed tests as successful and vice versa if some rule changes the result of a test (e.g.ErrorCollectororExpectedException).- Since:
- 4.9
-
-
Constructor Summary
Constructors Constructor Description TestWatcher()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Statementapply(Statement base, Description description)Modifies the method-runningStatementto implement this test-running rule.protected voidfailed(java.lang.Throwable e, Description description)Invoked when a test failsprivate voidfailedQuietly(java.lang.Throwable e, Description description, java.util.List<java.lang.Throwable> errors)protected voidfinished(Description description)Invoked when a test method finishes (whether passing or failing)private voidfinishedQuietly(Description description, java.util.List<java.lang.Throwable> errors)protected voidskipped(AssumptionViolatedException e, Description description)Invoked when a test is skipped due to a failed assumption.protected voidskipped(AssumptionViolatedException e, Description description)Deprecated.private voidskippedQuietly(AssumptionViolatedException e, Description description, java.util.List<java.lang.Throwable> errors)protected voidstarting(Description description)Invoked when a test is about to startprivate voidstartingQuietly(Description description, java.util.List<java.lang.Throwable> errors)protected voidsucceeded(Description description)Invoked when a test succeedsprivate voidsucceededQuietly(Description description, java.util.List<java.lang.Throwable> errors)
-
-
-
Method Detail
-
apply
public Statement apply(Statement base, Description description)
Description copied from interface:TestRuleModifies 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.
-
succeededQuietly
private void succeededQuietly(Description description, java.util.List<java.lang.Throwable> errors)
-
failedQuietly
private void failedQuietly(java.lang.Throwable e, Description description, java.util.List<java.lang.Throwable> errors)
-
skippedQuietly
private void skippedQuietly(AssumptionViolatedException e, Description description, java.util.List<java.lang.Throwable> errors)
-
startingQuietly
private void startingQuietly(Description description, java.util.List<java.lang.Throwable> errors)
-
finishedQuietly
private void finishedQuietly(Description description, java.util.List<java.lang.Throwable> errors)
-
succeeded
protected void succeeded(Description description)
Invoked when a test succeeds
-
failed
protected void failed(java.lang.Throwable e, Description description)Invoked when a test fails
-
skipped
protected void skipped(AssumptionViolatedException e, Description description)
Invoked when a test is skipped due to a failed assumption.
-
skipped
@Deprecated protected void skipped(AssumptionViolatedException e, Description description)
Deprecated.Invoked when a test is skipped due to a failed assumption.
-
starting
protected void starting(Description description)
Invoked when a test is about to start
-
finished
protected void finished(Description description)
Invoked when a test method finishes (whether passing or failing)
-
-