aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/junit/rules/TestWatchman.java
blob: c8d6c71ab7cfa2f066aa37797d1ac04ae1f372a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package org.junit.rules;

import org.junit.internal.AssumptionViolatedException;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

/**
 * TestWatchman 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:
 *
 * <pre>
 * public static class WatchmanTest {
 *  private static String watchedLog;
 *
 *  &#064;Rule
 *  public MethodRule watchman= new TestWatchman() {
 *      &#064;Override
 *      public void failed(Throwable e, FrameworkMethod method) {
 *          watchedLog+= method.getName() + &quot; &quot; + e.getClass().getSimpleName()
 *                  + &quot;\n&quot;;
 *         }
 *
 *      &#064;Override
 *      public void succeeded(FrameworkMethod method) {
 *          watchedLog+= method.getName() + &quot; &quot; + &quot;success!\n&quot;;
 *         }
 *     };
 *
 *  &#064;Test
 *  public void fails() {
 *      fail();
 *     }
 *
 *  &#064;Test
 *  public void succeeds() {
 *     }
 * }
 * </pre>
 *
 * @since 4.7
 * @deprecated Use {@link TestWatcher} (which implements {@link TestRule}) instead.
 */
@Deprecated
public class TestWatchman implements MethodRule {
    public Statement apply(final Statement base, final FrameworkMethod method,
            Object target) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                starting(method);
                try {
                    base.evaluate();
                    succeeded(method);
                } catch (AssumptionViolatedException e) {
                    throw e;
                } catch (Throwable e) {
                    failed(e, method);
                    throw e;
                } finally {
                    finished(method);
                }
            }
        };
    }

    /**
     * Invoked when a test method succeeds
     */
    public void succeeded(FrameworkMethod method) {
    }

    /**
     * Invoked when a test method fails
     */
    public void failed(Throwable e, FrameworkMethod method) {
    }

    /**
     * Invoked when a test method is about to start
     */
    public void starting(FrameworkMethod method) {
    }


    /**
     * Invoked when a test method finishes (whether passing or failing)
     */
    public void finished(FrameworkMethod method) {
    }
}