|
14 | 14 | import java.util.concurrent.Executor; |
15 | 15 |
|
16 | 16 | import static junit.framework.Assert.assertEquals; |
| 17 | +import static junit.framework.Assert.assertNotNull; |
17 | 18 | import static junit.framework.Assert.assertTrue; |
18 | 19 | import static org.mockito.Matchers.any; |
| 20 | +import static org.mockito.Matchers.anyString; |
19 | 21 | import static org.mockito.Matchers.eq; |
20 | 22 | import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.never; |
21 | 24 | import static org.mockito.Mockito.times; |
22 | 25 | import static org.mockito.Mockito.verify; |
| 26 | +import static org.mockito.Mockito.when; |
23 | 27 |
|
24 | 28 | @RunWith(RobolectricGradleTestRunner.class) |
25 | 29 | @Config(constants = BuildConfig.class, sdk = 21) |
@@ -79,6 +83,34 @@ public void testUnsubscribeWhenSubscribedToCallback() throws Exception { |
79 | 83 | verify(unsubscribeMockCallback, times(1)).onUnsubscribe(parseQuery); |
80 | 84 | } |
81 | 85 |
|
| 86 | + @Test |
| 87 | + public void testErrorWhileSubscribing() throws Exception { |
| 88 | + ParseQuery.State state = mock(ParseQuery.State.class); |
| 89 | + when(state.toJSON(any(ParseEncoder.class))).thenThrow(new RuntimeException("forced error")); |
| 90 | + |
| 91 | + ParseQuery.State.Builder builder = mock(ParseQuery.State.Builder.class); |
| 92 | + when(builder.build()).thenReturn(state); |
| 93 | + ParseQuery query = mock(ParseQuery.class); |
| 94 | + when(query.getBuilder()).thenReturn(builder); |
| 95 | + |
| 96 | + SubscriptionHandling handling = parseLiveQueryClient.subscribe(query); |
| 97 | + |
| 98 | + SubscriptionHandling.HandleErrorCallback<ParseObject> errorMockCallback = mock(SubscriptionHandling.HandleErrorCallback.class); |
| 99 | + handling.handleError(errorMockCallback); |
| 100 | + |
| 101 | + // Trigger a re-subscribe |
| 102 | + webSocketClientCallback.onMessage(createConnectedMessage().toString()); |
| 103 | + |
| 104 | + // This will never get a chance to call op=subscribe, because an exception was thrown |
| 105 | + verify(webSocketClient, never()).send(anyString()); |
| 106 | + |
| 107 | + ArgumentCaptor<LiveQueryException> errorCaptor = ArgumentCaptor.forClass(LiveQueryException.class); |
| 108 | + verify(errorMockCallback, times(1)).onError(eq(query), errorCaptor.capture()); |
| 109 | + |
| 110 | + assertEquals("Error when subscribing", errorCaptor.getValue().getMessage()); |
| 111 | + assertNotNull(errorCaptor.getValue().getCause()); |
| 112 | + } |
| 113 | + |
82 | 114 | @Test |
83 | 115 | public void testErrorWhenSubscribedToCallback() throws Exception { |
84 | 116 | ParseQuery<ParseObject> parseQuery = new ParseQuery<>("test"); |
|
0 commit comments