|
18 | 18 | import static org.junit.Assert.fail;
|
19 | 19 | import static org.mockito.Mockito.mock;
|
20 | 20 | import static org.mockito.Mockito.verify;
|
| 21 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 22 | +import static org.mockito.Mockito.verifyZeroInteractions; |
21 | 23 |
|
22 | 24 | import java.util.ArrayList;
|
23 | 25 | import java.util.List;
|
24 | 26 | import java.util.concurrent.CountDownLatch;
|
25 | 27 |
|
26 | 28 | import org.junit.Before;
|
27 | 29 | import org.junit.Test;
|
28 |
| -import org.mockito.MockitoAnnotations; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | +import org.mockito.runners.MockitoJUnitRunner; |
29 | 32 |
|
30 | 33 | import rx.Subscription;
|
31 | 34 |
|
| 35 | +@RunWith(MockitoJUnitRunner.class) |
32 | 36 | public class SerialSubscriptionTests {
|
33 | 37 | private SerialSubscription serialSubscription;
|
34 | 38 |
|
35 | 39 | @Before
|
36 | 40 | public void setUp() {
|
37 |
| - MockitoAnnotations.initMocks(this); |
38 |
| - |
39 | 41 | serialSubscription = new SerialSubscription();
|
40 | 42 | }
|
41 | 43 |
|
42 | 44 | @Test
|
43 | 45 | public void unsubscribingWithoutUnderlyingDoesNothing() {
|
44 | 46 | serialSubscription.unsubscribe();
|
45 | 47 | }
|
| 48 | + |
| 49 | + @Test |
| 50 | + public void unsubscribingTwiceDoesUnsubscribeOnce() { |
| 51 | + Subscription underlying = mock(Subscription.class); |
| 52 | + serialSubscription.setSubscription(underlying); |
| 53 | + |
| 54 | + serialSubscription.unsubscribe(); |
| 55 | + verify(underlying).unsubscribe(); |
| 56 | + |
| 57 | + serialSubscription.unsubscribe(); |
| 58 | + verifyNoMoreInteractions(underlying); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void settingSameSubscriptionTwiceDoesUnsubscribeIt() { |
| 63 | + Subscription underlying = mock(Subscription.class); |
| 64 | + serialSubscription.setSubscription(underlying); |
| 65 | + verifyZeroInteractions(underlying); |
| 66 | + serialSubscription.setSubscription(underlying); |
| 67 | + verify(underlying).unsubscribe();; |
| 68 | + } |
46 | 69 |
|
47 | 70 | @Test
|
48 | 71 | public void unsubscribingWithSingleUnderlyingUnsubscribes() {
|
@@ -117,7 +140,7 @@ public void run() {
|
117 | 140 | verify(underlying).unsubscribe();
|
118 | 141 |
|
119 | 142 | for (final Thread t : threads) {
|
120 |
| - t.interrupt(); |
| 143 | + t.join(); |
121 | 144 | }
|
122 | 145 | }
|
123 | 146 | }
|
0 commit comments