Package org.junit.rules
Class TestWatcher
java.lang.Object
org.junit.rules.TestWatcher
- All Implemented Interfaces:
TestRule
- Direct Known Subclasses:
Stopwatch.InternalWatcher
,TestName
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 order
of the
TestWatcher
to Integer.MIN_VALUE
so 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. ErrorCollector
or
ExpectedException
).
- Since:
- 4.9
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionapply
(Statement base, Description description) Modifies the method-runningStatement
to implement this test-running rule.protected void
failed
(Throwable e, Description description) Invoked when a test failsprivate void
failedQuietly
(Throwable e, Description description, List<Throwable> errors) protected void
finished
(Description description) Invoked when a test method finishes (whether passing or failing)private void
finishedQuietly
(Description description, List<Throwable> errors) protected void
skipped
(AssumptionViolatedException e, Description description) Invoked when a test is skipped due to a failed assumption.protected void
skipped
(AssumptionViolatedException e, Description description) Deprecated.private void
skippedQuietly
(AssumptionViolatedException e, Description description, List<Throwable> errors) protected void
starting
(Description description) Invoked when a test is about to startprivate void
startingQuietly
(Description description, List<Throwable> errors) protected void
succeeded
(Description description) Invoked when a test succeedsprivate void
succeededQuietly
(Description description, List<Throwable> errors)
-
Constructor Details
-
TestWatcher
public TestWatcher()
-
-
Method Details
-
apply
Description copied from interface:TestRule
Modifies the method-runningStatement
to implement this test-running rule.- Specified by:
apply
in interfaceTestRule
- Parameters:
base
- TheStatement
to be modifieddescription
- ADescription
of the test implemented inbase
- Returns:
- a new statement, which may be the same as
base
, a wrapper aroundbase
, or a completely new Statement.
-
succeededQuietly
-
failedQuietly
-
skippedQuietly
private void skippedQuietly(AssumptionViolatedException e, Description description, List<Throwable> errors) -
startingQuietly
-
finishedQuietly
-
succeeded
Invoked when a test succeeds -
failed
Invoked when a test fails -
skipped
Invoked when a test is skipped due to a failed assumption. -
skipped
Deprecated.Invoked when a test is skipped due to a failed assumption. -
starting
Invoked when a test is about to start -
finished
Invoked when a test method finishes (whether passing or failing)
-
skipped(AssumptionViolatedException, Description)