• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.junit.rules;
2 
3 import org.junit.internal.AssumptionViolatedException;
4 import org.junit.runners.model.FrameworkMethod;
5 import org.junit.runners.model.Statement;
6 
7 /**
8  * TestWatchman is a base class for Rules that take note of the testing
9  * action, without modifying it. For example, this class will keep a log of each
10  * passing and failing test:
11  *
12  * <pre>
13  * public static class WatchmanTest {
14  *  private static String watchedLog;
15  *
16  *  &#064;Rule
17  *  public MethodRule watchman= new TestWatchman() {
18  *      &#064;Override
19  *      public void failed(Throwable e, FrameworkMethod method) {
20  *          watchedLog+= method.getName() + &quot; &quot; + e.getClass().getSimpleName()
21  *                  + &quot;\n&quot;;
22  *         }
23  *
24  *      &#064;Override
25  *      public void succeeded(FrameworkMethod method) {
26  *          watchedLog+= method.getName() + &quot; &quot; + &quot;success!\n&quot;;
27  *         }
28  *     };
29  *
30  *  &#064;Test
31  *  public void fails() {
32  *      fail();
33  *     }
34  *
35  *  &#064;Test
36  *  public void succeeds() {
37  *     }
38  * }
39  * </pre>
40  *
41  * @since 4.7
42  * @deprecated Use {@link TestWatcher} (which implements {@link TestRule}) instead.
43  */
44 @Deprecated
45 public class TestWatchman implements MethodRule {
apply(final Statement base, final FrameworkMethod method, Object target)46     public Statement apply(final Statement base, final FrameworkMethod method,
47             Object target) {
48         return new Statement() {
49             @Override
50             public void evaluate() throws Throwable {
51                 starting(method);
52                 try {
53                     base.evaluate();
54                     succeeded(method);
55                 } catch (AssumptionViolatedException e) {
56                     throw e;
57                 } catch (Throwable e) {
58                     failed(e, method);
59                     throw e;
60                 } finally {
61                     finished(method);
62                 }
63             }
64         };
65     }
66 
67     /**
68      * Invoked when a test method succeeds
69      */
70     public void succeeded(FrameworkMethod method) {
71     }
72 
73     /**
74      * Invoked when a test method fails
75      */
76     public void failed(Throwable e, FrameworkMethod method) {
77     }
78 
79     /**
80      * Invoked when a test method is about to start
81      */
82     public void starting(FrameworkMethod method) {
83     }
84 
85 
86     /**
87      * Invoked when a test method finishes (whether passing or failing)
88      */
89     public void finished(FrameworkMethod method) {
90     }
91 }
92