org.junit.rules
Class TestWatcher

java.lang.Object
  extended by org.junit.rules.TestRule
      extended by org.junit.rules.TestWatcher
Direct Known Subclasses:
TestName

public abstract class TestWatcher
extends 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
        public MethodRule watchman= new TestWatcher() {
                @Override
                public void failed(Description d) {
                        watchedLog+= d + "\n";
                }
 
                @Override
                public void succeeded(Description d) {
                        watchedLog+= d + " " + "success!\n";
                }
        };
 
        @Test
        public void fails() {
                fail();
        }
 
        @Test
        public void succeeds() {
        }
 }
 


Constructor Summary
TestWatcher()
           
 
Method Summary
protected  Statement apply(Statement base, Description description)
          Modifies the method-running Statement to implement this test-running rule.
 void failed(Throwable e, Description description)
          Invoked when a test fails
 void finished(Description description)
          Invoked when a test method finishes (whether passing or failing)
 void starting(Description description)
          Invoked when a test is about to start
 void succeeded(Description description)
          Invoked when a test succeeds
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TestWatcher

public TestWatcher()
Method Detail

apply

protected Statement apply(Statement base,
                          Description description)
Description copied from class: TestRule
Modifies the method-running Statement to implement this test-running rule.

Specified by:
apply in class TestRule
Parameters:
base - The Statement to be modified
description - A Description of the test implemented in base
Returns:
a new statement, which may be the same as base, a wrapper around base, or a completely new Statement.

succeeded

public void succeeded(Description description)
Invoked when a test succeeds

Parameters:
description -

failed

public void failed(Throwable e,
                   Description description)
Invoked when a test fails

Parameters:
e -
description -

starting

public void starting(Description description)
Invoked when a test is about to start

Parameters:
description -

finished

public void finished(Description description)
Invoked when a test method finishes (whether passing or failing)

Parameters:
description -