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 * @Rule 17 * public MethodRule watchman= new TestWatchman() { 18 * @Override 19 * public void failed(Throwable e, FrameworkMethod method) { 20 * watchedLog+= method.getName() + " " + e.getClass().getSimpleName() 21 * + "\n"; 22 * } 23 * 24 * @Override 25 * public void succeeded(FrameworkMethod method) { 26 * watchedLog+= method.getName() + " " + "success!\n"; 27 * } 28 * }; 29 * 30 * @Test 31 * public void fails() { 32 * fail(); 33 * } 34 * 35 * @Test 36 * public void succeeds() { 37 * } 38 * } 39 * </pre> 40 * 41 * @deprecated {@link MethodRule} is deprecated. 42 * Use {@link TestWatcher} implements {@link TestRule} instead. 43 */ 44 @Deprecated 45 public class TestWatchman implements MethodRule { 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 t) { 58 failed(t, method); 59 throw t; 60 } finally { 61 finished(method); 62 } 63 } 64 }; 65 } 66 67 /** 68 * Invoked when a test method succeeds 69 * 70 * @param method 71 */ 72 public void succeeded(FrameworkMethod method) { 73 } 74 75 /** 76 * Invoked when a test method fails 77 * 78 * @param e 79 * @param method 80 */ 81 public void failed(Throwable e, FrameworkMethod method) { 82 } 83 84 /** 85 * Invoked when a test method is about to start 86 * 87 * @param method 88 */ 89 public void starting(FrameworkMethod method) { 90 } 91 92 93 /** 94 * Invoked when a test method finishes (whether passing or failing) 95 * 96 * @param method 97 */ 98 public void finished(FrameworkMethod method) { 99 } 100 } 101