001 package org.junit.rules;
002
003 import org.junit.internal.AssumptionViolatedException;
004 import org.junit.runners.model.FrameworkMethod;
005 import org.junit.runners.model.Statement;
006
007 /**
008 * TestWatchman is a base class for Rules that take note of the testing
009 * action, without modifying it. For example, this class will keep a log of each
010 * passing and failing test:
011 *
012 * <pre>
013 * public static class WatchmanTest {
014 * private static String watchedLog;
015 *
016 * @Rule
017 * public MethodRule watchman= new TestWatchman() {
018 * @Override
019 * public void failed(Throwable e, FrameworkMethod method) {
020 * watchedLog+= method.getName() + " " + e.getClass().getSimpleName()
021 * + "\n";
022 * }
023 *
024 * @Override
025 * public void succeeded(FrameworkMethod method) {
026 * watchedLog+= method.getName() + " " + "success!\n";
027 * }
028 * };
029 *
030 * @Test
031 * public void fails() {
032 * fail();
033 * }
034 *
035 * @Test
036 * public void succeeds() {
037 * }
038 * }
039 * </pre>
040 *
041 * @since 4.7
042 * @deprecated Use {@link TestWatcher} (which implements {@link TestRule}) instead.
043 */
044 @Deprecated
045 public class TestWatchman implements MethodRule {
046 public Statement apply(final Statement base, final FrameworkMethod method,
047 Object target) {
048 return new Statement() {
049 @Override
050 public void evaluate() throws Throwable {
051 starting(method);
052 try {
053 base.evaluate();
054 succeeded(method);
055 } catch (AssumptionViolatedException e) {
056 throw e;
057 } catch (Throwable e) {
058 failed(e, method);
059 throw e;
060 } finally {
061 finished(method);
062 }
063 }
064 };
065 }
066
067 /**
068 * Invoked when a test method succeeds
069 */
070 public void succeeded(FrameworkMethod method) {
071 }
072
073 /**
074 * Invoked when a test method fails
075 */
076 public void failed(Throwable e, FrameworkMethod method) {
077 }
078
079 /**
080 * Invoked when a test method is about to start
081 */
082 public void starting(FrameworkMethod method) {
083 }
084
085
086 /**
087 * Invoked when a test method finishes (whether passing or failing)
088 */
089 public void finished(FrameworkMethod method) {
090 }
091 }