|
18 | 18 | import bolts.Task;
|
19 | 19 |
|
20 | 20 | import static junit.framework.Assert.assertEquals;
|
| 21 | +import static junit.framework.Assert.assertNotNull; |
21 | 22 | import static junit.framework.Assert.assertTrue;
|
22 | 23 | import static org.mockito.AdditionalMatchers.and;
|
23 | 24 | import static org.mockito.AdditionalMatchers.not;
|
24 | 25 | import static org.mockito.Matchers.any;
|
| 26 | +import static org.mockito.Matchers.anyString; |
25 | 27 | import static org.mockito.Matchers.anyBoolean;
|
26 | 28 | import static org.mockito.Matchers.contains;
|
27 | 29 | import static org.mockito.Matchers.eq;
|
28 | 30 | import static org.mockito.Mockito.mock;
|
| 31 | +import static org.mockito.Mockito.never; |
29 | 32 | import static org.mockito.Mockito.times;
|
30 | 33 | import static org.mockito.Mockito.verify;
|
31 | 34 | import static org.mockito.Mockito.when;
|
@@ -110,6 +113,34 @@ public void testUnsubscribeWhenSubscribedToCallback() throws Exception {
|
110 | 113 | verify(unsubscribeMockCallback, times(1)).onUnsubscribe(parseQuery);
|
111 | 114 | }
|
112 | 115 |
|
| 116 | + @Test |
| 117 | + public void testErrorWhileSubscribing() throws Exception { |
| 118 | + ParseQuery.State state = mock(ParseQuery.State.class); |
| 119 | + when(state.toJSON(any(ParseEncoder.class))).thenThrow(new RuntimeException("forced error")); |
| 120 | + |
| 121 | + ParseQuery.State.Builder builder = mock(ParseQuery.State.Builder.class); |
| 122 | + when(builder.build()).thenReturn(state); |
| 123 | + ParseQuery query = mock(ParseQuery.class); |
| 124 | + when(query.getBuilder()).thenReturn(builder); |
| 125 | + |
| 126 | + SubscriptionHandling handling = parseLiveQueryClient.subscribe(query); |
| 127 | + |
| 128 | + SubscriptionHandling.HandleErrorCallback<ParseObject> errorMockCallback = mock(SubscriptionHandling.HandleErrorCallback.class); |
| 129 | + handling.handleError(errorMockCallback); |
| 130 | + |
| 131 | + // Trigger a re-subscribe |
| 132 | + webSocketClientCallback.onMessage(createConnectedMessage().toString()); |
| 133 | + |
| 134 | + // This will never get a chance to call op=subscribe, because an exception was thrown |
| 135 | + verify(webSocketClient, never()).send(anyString()); |
| 136 | + |
| 137 | + ArgumentCaptor<LiveQueryException> errorCaptor = ArgumentCaptor.forClass(LiveQueryException.class); |
| 138 | + verify(errorMockCallback, times(1)).onError(eq(query), errorCaptor.capture()); |
| 139 | + |
| 140 | + assertEquals("Error when subscribing", errorCaptor.getValue().getMessage()); |
| 141 | + assertNotNull(errorCaptor.getValue().getCause()); |
| 142 | + } |
| 143 | + |
113 | 144 | @Test
|
114 | 145 | public void testErrorWhenSubscribedToCallback() throws Exception {
|
115 | 146 | ParseQuery<ParseObject> parseQuery = new ParseQuery<>("test");
|
|
0 commit comments