|
16 | 16 | import static org.junit.Assert.assertSame;
|
17 | 17 | import static org.junit.Assert.assertTrue;
|
18 | 18 | import static org.junit.Assert.fail;
|
| 19 | +import static org.mockito.Matchers.eq; |
19 | 20 | import static org.mockito.Mockito.doThrow;
|
20 | 21 | import static org.mockito.Mockito.mock;
|
21 | 22 | import static org.mockito.Mockito.verify;
|
@@ -570,4 +571,81 @@ public void shouldPassErrorFromCallable() throws Exception {
|
570 | 571 |
|
571 | 572 | verify(callable).call();
|
572 | 573 | }
|
| 574 | + |
| 575 | + @Test |
| 576 | + public void doOnSuccessShouldInvokeAction() { |
| 577 | + Action1<String> action = mock(Action1.class); |
| 578 | + |
| 579 | + TestSubscriber<String> testSubscriber = new TestSubscriber<String>(); |
| 580 | + |
| 581 | + Single |
| 582 | + .just("value") |
| 583 | + .doOnSuccess(action) |
| 584 | + .subscribe(testSubscriber); |
| 585 | + |
| 586 | + testSubscriber.assertValue("value"); |
| 587 | + testSubscriber.assertNoErrors(); |
| 588 | + |
| 589 | + verify(action).call(eq("value")); |
| 590 | + } |
| 591 | + |
| 592 | + @Test |
| 593 | + public void doOnSuccessShouldPassErrorFromActionToSubscriber() { |
| 594 | + Action1<String> action = mock(Action1.class); |
| 595 | + |
| 596 | + Throwable error = new IllegalStateException(); |
| 597 | + doThrow(error).when(action).call(eq("value")); |
| 598 | + |
| 599 | + TestSubscriber<String> testSubscriber = new TestSubscriber<String>(); |
| 600 | + |
| 601 | + Single |
| 602 | + .just("value") |
| 603 | + .doOnSuccess(action) |
| 604 | + .subscribe(testSubscriber); |
| 605 | + |
| 606 | + testSubscriber.assertNoValues(); |
| 607 | + testSubscriber.assertError(error); |
| 608 | + |
| 609 | + verify(action).call(eq("value")); |
| 610 | + } |
| 611 | + |
| 612 | + @Test |
| 613 | + public void doOnSuccessShouldNotCallActionIfSingleThrowsError() { |
| 614 | + Action1<Object> action = mock(Action1.class); |
| 615 | + |
| 616 | + Throwable error = new IllegalStateException(); |
| 617 | + |
| 618 | + TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>(); |
| 619 | + |
| 620 | + Single |
| 621 | + .error(error) |
| 622 | + .doOnSuccess(action) |
| 623 | + .subscribe(testSubscriber); |
| 624 | + |
| 625 | + testSubscriber.assertNoValues(); |
| 626 | + testSubscriber.assertError(error); |
| 627 | + |
| 628 | + verifyZeroInteractions(action); |
| 629 | + } |
| 630 | + |
| 631 | + @Test |
| 632 | + public void doOnSuccessShouldNotSwallowExceptionThrownByAction() { |
| 633 | + Action1<String> action = mock(Action1.class); |
| 634 | + |
| 635 | + Throwable exceptionFromAction = new IllegalStateException(); |
| 636 | + |
| 637 | + doThrow(exceptionFromAction).when(action).call(eq("value")); |
| 638 | + |
| 639 | + TestSubscriber<String> testSubscriber = new TestSubscriber<String>(); |
| 640 | + |
| 641 | + Single |
| 642 | + .just("value") |
| 643 | + .doOnSuccess(action) |
| 644 | + .subscribe(testSubscriber); |
| 645 | + |
| 646 | + testSubscriber.assertNoValues(); |
| 647 | + testSubscriber.assertError(exceptionFromAction); |
| 648 | + |
| 649 | + verify(action).call(eq("value")); |
| 650 | + } |
573 | 651 | }
|
0 commit comments