|
7 | 7 | import static org.junit.experimental.results.PrintableResult.testResult;
|
8 | 8 | import static org.junit.experimental.results.ResultMatchers.failureCountIs;
|
9 | 9 | import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;
|
| 10 | +import static org.junit.rules.ExpectedException.none; |
10 | 11 |
|
11 | 12 | import org.junit.Rule;
|
12 | 13 | import org.junit.Test;
|
@@ -342,4 +343,55 @@ public void finished() {
|
342 | 343 | Finished.catchedDescription.getDisplayName());
|
343 | 344 | }
|
344 | 345 | }
|
| 346 | + |
| 347 | + //The following tests check the information in TestWatcher's Javadoc |
| 348 | + //regarding interplay with other rules. |
| 349 | + public static class InterplayWithOtherRules { |
| 350 | + private static StringBuilder log; |
| 351 | + |
| 352 | + public static class ExpectedExceptionTest { |
| 353 | + @Rule(order = Integer.MIN_VALUE) |
| 354 | + //the field name must be alphabetically lower than "thrown" in order |
| 355 | + //to make the test failing if order is not set |
| 356 | + public final TestRule a = new LoggingTestWatcher(log); |
| 357 | + |
| 358 | + @Rule |
| 359 | + public final ExpectedException thrown = none(); |
| 360 | + |
| 361 | + @Test |
| 362 | + public void testWithExpectedException() { |
| 363 | + thrown.expect(RuntimeException.class); |
| 364 | + throw new RuntimeException("expected exception"); |
| 365 | + } |
| 366 | + } |
| 367 | + |
| 368 | + @Test |
| 369 | + public void expectedExceptionIsSeenAsSuccessfulTest() { |
| 370 | + log = new StringBuilder(); |
| 371 | + JUnitCore.runClasses(ExpectedExceptionTest.class); |
| 372 | + assertEquals("starting succeeded finished ", log.toString()); |
| 373 | + } |
| 374 | + |
| 375 | + public static class ErrorCollectorTest { |
| 376 | + @Rule(order = Integer.MIN_VALUE) |
| 377 | + //the field name must be alphabetically lower than "collector" in |
| 378 | + //order to make the test failing if order is not set |
| 379 | + public final TestRule a = new LoggingTestWatcher(log); |
| 380 | + |
| 381 | + @Rule |
| 382 | + public final ErrorCollector collector = new ErrorCollector(); |
| 383 | + |
| 384 | + @Test |
| 385 | + public void test() { |
| 386 | + collector.addError(new RuntimeException("expected exception")); |
| 387 | + } |
| 388 | + } |
| 389 | + |
| 390 | + @Test |
| 391 | + public void testIsSeenAsFailedBecauseOfCollectedError() { |
| 392 | + log = new StringBuilder(); |
| 393 | + JUnitCore.runClasses(ErrorCollectorTest.class); |
| 394 | + assertEquals("starting failed finished ", log.toString()); |
| 395 | + } |
| 396 | + } |
345 | 397 | }
|
0 commit comments