1 package org.junit.tests.experimental.rules; 2 3 import static junit.framework.Assert.fail; 4 import static org.hamcrest.CoreMatchers.is; 5 import static org.junit.Assert.assertThat; 6 import static org.junit.Assume.assumeTrue; 7 import static org.junit.runner.JUnitCore.runClasses; 8 9 import org.junit.Rule; 10 import org.junit.Test; 11 import org.junit.rules.TestWatchman; 12 import org.junit.runners.model.FrameworkMethod; 13 14 @SuppressWarnings("deprecation") 15 public class TestWatchmanTest { 16 public static class ViolatedAssumptionTest { 17 @Rule 18 public static LoggingTestWatchman watchman = new LoggingTestWatchman(); 19 20 @Test 21 public void succeeds() { 22 assumeTrue(false); 23 } 24 } 25 26 @Test 27 public void neitherLogSuccessNorFailedForViolatedAssumption() { 28 runClasses(ViolatedAssumptionTest.class); 29 assertThat(ViolatedAssumptionTest.watchman.log.toString(), 30 is("starting finished ")); 31 } 32 33 public static class FailingTest { 34 @Rule 35 public static LoggingTestWatchman watchman = new LoggingTestWatchman(); 36 37 @Test 38 public void succeeds() { 39 fail(); 40 } 41 } 42 43 @Test 44 public void logFailingTest() { 45 runClasses(FailingTest.class); 46 assertThat(FailingTest.watchman.log.toString(), 47 is("starting failed finished ")); 48 } 49 50 private static class LoggingTestWatchman extends TestWatchman { 51 private final StringBuilder log = new StringBuilder(); 52 53 @Override 54 public void succeeded(FrameworkMethod method) { 55 log.append("succeeded "); 56 } 57 58 @Override 59 public void failed(Throwable e, FrameworkMethod method) { 60 log.append("failed "); 61 } 62 63 @Override 64 public void starting(FrameworkMethod method) { 65 log.append("starting "); 66 } 67 68 @Override 69 public void finished(FrameworkMethod method) { 70 log.append("finished "); 71 } 72 } 73 }