|
13 | 13 | package rx;
|
14 | 14 |
|
15 | 15 | import static org.junit.Assert.assertEquals;
|
| 16 | +import static org.junit.Assert.assertSame; |
16 | 17 | import static org.junit.Assert.assertTrue;
|
17 | 18 | import static org.junit.Assert.fail;
|
| 19 | +import static org.mockito.Mockito.doThrow; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.verify; |
| 22 | +import static org.mockito.Mockito.verifyZeroInteractions; |
18 | 23 |
|
19 | 24 | import java.util.Arrays;
|
20 | 25 | import java.util.concurrent.CountDownLatch;
|
|
26 | 31 | import org.junit.Test;
|
27 | 32 |
|
28 | 33 | import rx.Single.OnSubscribe;
|
| 34 | +import rx.exceptions.CompositeException; |
29 | 35 | import rx.functions.Action0;
|
| 36 | +import rx.functions.Action1; |
30 | 37 | import rx.functions.Func1;
|
31 | 38 | import rx.functions.Func2;
|
32 | 39 | import rx.observers.TestSubscriber;
|
@@ -461,4 +468,66 @@ public void testToObservable() {
|
461 | 468 | ts.assertValue("a");
|
462 | 469 | ts.assertCompleted();
|
463 | 470 | }
|
| 471 | + |
| 472 | + @Test |
| 473 | + public void doOnErrorShouldNotCallActionIfNoErrorHasOccurred() { |
| 474 | + Action1<Throwable> action = mock(Action1.class); |
| 475 | + |
| 476 | + TestSubscriber<String> testSubscriber = new TestSubscriber<String>(); |
| 477 | + |
| 478 | + Single |
| 479 | + .just("value") |
| 480 | + .doOnError(action) |
| 481 | + .subscribe(testSubscriber); |
| 482 | + |
| 483 | + testSubscriber.assertValue("value"); |
| 484 | + testSubscriber.assertNoErrors(); |
| 485 | + |
| 486 | + verifyZeroInteractions(action); |
| 487 | + } |
| 488 | + |
| 489 | + @Test |
| 490 | + public void doOnErrorShouldCallActionIfErrorHasOccurred() { |
| 491 | + Action1<Throwable> action = mock(Action1.class); |
| 492 | + |
| 493 | + TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>(); |
| 494 | + |
| 495 | + Throwable error = new IllegalStateException(); |
| 496 | + |
| 497 | + Single |
| 498 | + .error(error) |
| 499 | + .doOnError(action) |
| 500 | + .subscribe(testSubscriber); |
| 501 | + |
| 502 | + testSubscriber.assertNoValues(); |
| 503 | + testSubscriber.assertError(error); |
| 504 | + |
| 505 | + verify(action).call(error); |
| 506 | + } |
| 507 | + |
| 508 | + @Test |
| 509 | + public void doOnErrorShouldThrowCompositeExceptionIfOnErrorActionThrows() { |
| 510 | + Action1<Throwable> action = mock(Action1.class); |
| 511 | + |
| 512 | + |
| 513 | + Throwable error = new RuntimeException(); |
| 514 | + Throwable exceptionFromOnErrorAction = new IllegalStateException(); |
| 515 | + doThrow(exceptionFromOnErrorAction).when(action).call(error); |
| 516 | + |
| 517 | + TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>(); |
| 518 | + |
| 519 | + Single |
| 520 | + .error(error) |
| 521 | + .doOnError(action) |
| 522 | + .subscribe(testSubscriber); |
| 523 | + |
| 524 | + testSubscriber.assertNoValues(); |
| 525 | + CompositeException compositeException = (CompositeException) testSubscriber.getOnErrorEvents().get(0); |
| 526 | + |
| 527 | + assertEquals(2, compositeException.getExceptions().size()); |
| 528 | + assertSame(error, compositeException.getExceptions().get(0)); |
| 529 | + assertSame(exceptionFromOnErrorAction, compositeException.getExceptions().get(1)); |
| 530 | + |
| 531 | + verify(action).call(error); |
| 532 | + } |
464 | 533 | }
|
0 commit comments