View Javadoc
1   package org.junit.tests.experimental.rules;
2   
3   import static org.hamcrest.CoreMatchers.is;
4   import static org.junit.Assert.assertThat;
5   import static org.junit.Assert.fail;
6   import static org.junit.Assume.assumeTrue;
7   import static org.junit.experimental.results.PrintableResult.testResult;
8   import static org.junit.experimental.results.ResultMatchers.failureCountIs;
9   import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;
10  import static org.junit.runner.JUnitCore.runClasses;
11  import org.junit.Rule;
12  import org.junit.Test;
13  import org.junit.experimental.results.PrintableResult;
14  import org.junit.internal.AssumptionViolatedException;
15  import org.junit.rules.TestRule;
16  import org.junit.rules.TestWatcher;
17  import org.junit.runner.Description;
18  
19  public class TestWatcherTest {
20      public static class ViolatedAssumptionTest {
21          private static StringBuilder watchedLog = new StringBuilder();
22  
23          @Rule
24          public TestRule watcher = new LoggingTestWatcher(watchedLog);
25  
26          @Test
27          public void succeeds() {
28              assumeTrue(false);
29          }
30      }
31  
32      @Test
33      public void neitherLogSuccessNorFailedForViolatedAssumption() {
34          ViolatedAssumptionTest.watchedLog = new StringBuilder();
35          runClasses(ViolatedAssumptionTest.class);
36          assertThat(ViolatedAssumptionTest.watchedLog.toString(),
37                  is("starting skipped finished "));
38      }
39  
40      public static class InternalViolatedAssumptionTest {
41          private static StringBuilder watchedLog = new StringBuilder();
42  
43          @Rule
44          public TestRule watcher = new TestWatcher() {
45              @Override
46              protected void starting(Description description) {
47                  watchedLog.append("starting ");
48              }
49  
50              @Override
51              protected void finished(Description description) {
52                  watchedLog.append("finished ");
53              }
54  
55              protected void skipped(AssumptionViolatedException e, Description description) {
56                  watchedLog.append("skipped ");
57              }
58          };
59  
60          @Test
61          public void succeeds() {
62              throw new AssumptionViolatedException("don't run");
63          }
64      }
65  
66      @Test
67      public void internalViolatedAssumption() {
68          InternalViolatedAssumptionTest.watchedLog = new StringBuilder();
69          runClasses(InternalViolatedAssumptionTest.class);
70          assertThat(InternalViolatedAssumptionTest.watchedLog.toString(),
71                  is("starting skipped finished "));
72      }
73  
74      public static class TestWatcherSkippedThrowsExceptionTest {
75          @Rule
76          public TestRule watcher = new TestWatcher() {
77              @Override
78              protected void skipped(AssumptionViolatedException e, Description description) {
79                  throw new RuntimeException("watcher failure");
80              }
81          };
82  
83          @Test
84          public void fails() {
85              throw new AssumptionViolatedException("test failure");
86          }
87      }
88  
89      @Test
90      public void testWatcherSkippedThrowsException() {
91          PrintableResult result = testResult(TestWatcherSkippedThrowsExceptionTest.class);
92          assertThat(result, failureCountIs(2));
93          assertThat(result, hasFailureContaining("test failure"));
94          assertThat(result, hasFailureContaining("watcher failure"));
95      }
96  
97      public static class FailingTest {
98          private static StringBuilder watchedLog = new StringBuilder();
99  
100         @Rule
101         public TestRule watcher = new LoggingTestWatcher(watchedLog);
102 
103         @Test
104         public void succeeds() {
105             fail();
106         }
107     }
108 
109     @Test
110     public void logFailingTest() {
111         FailingTest.watchedLog = new StringBuilder();
112         runClasses(FailingTest.class);
113         assertThat(FailingTest.watchedLog.toString(),
114                 is("starting failed finished "));
115     }
116 
117     public static class TestWatcherFailedThrowsExceptionTest {
118         @Rule
119         public TestRule watcher = new TestWatcher() {
120             @Override
121             protected void failed(Throwable e, Description description) {
122                 throw new RuntimeException("watcher failure");
123             }
124         };
125 
126         @Test
127         public void fails() {
128             throw new IllegalArgumentException("test failure");
129         }
130     }
131 
132     @Test
133     public void testWatcherFailedThrowsException() {
134         PrintableResult result = testResult(TestWatcherFailedThrowsExceptionTest.class);
135         assertThat(result, failureCountIs(2));
136         assertThat(result, hasFailureContaining("test failure"));
137         assertThat(result, hasFailureContaining("watcher failure"));
138     }
139 
140     public static class TestWatcherStartingThrowsExceptionTest {
141         @Rule
142         public TestRule watcher = new TestWatcher() {
143             @Override
144             protected void starting(Description description) {
145                 throw new RuntimeException("watcher failure");
146             }
147         };
148 
149         @Test
150         public void fails() {
151             throw new IllegalArgumentException("test failure");
152         }
153     }
154 
155     @Test
156     public void testWatcherStartingThrowsException() {
157         PrintableResult result = testResult(TestWatcherStartingThrowsExceptionTest.class);
158         assertThat(result, failureCountIs(2));
159         assertThat(result, hasFailureContaining("test failure"));
160         assertThat(result, hasFailureContaining("watcher failure"));
161     }
162 
163     public static class TestWatcherFailedAndFinishedThrowsExceptionTest {
164         @Rule
165         public TestRule watcher = new TestWatcher() {
166             @Override
167             protected void failed(Throwable e, Description description) {
168                 throw new RuntimeException("watcher failed failure");
169             }
170 
171             @Override
172             protected void finished(Description description) {
173                 throw new RuntimeException("watcher finished failure");
174             }
175         };
176 
177         @Test
178         public void fails() {
179             throw new IllegalArgumentException("test failure");
180         }
181     }
182 
183     @Test
184     public void testWatcherFailedAndFinishedThrowsException() {
185         PrintableResult result = testResult(TestWatcherFailedAndFinishedThrowsExceptionTest.class);
186         assertThat(result, failureCountIs(3));
187         assertThat(result, hasFailureContaining("test failure"));
188         assertThat(result, hasFailureContaining("watcher failed failure"));
189         assertThat(result, hasFailureContaining("watcher finished failure"));
190     }
191 }