Skip to content

Commit 316eb7e

Browse files
author
Joe Hansche
committed
Expect onError() when an exception is raised during subscribe()
1 parent 894250c commit 316eb7e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

ParseLiveQuery/src/test/java/com/parse/TestParseLiveQueryClient.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
import java.util.concurrent.Executor;
1515

1616
import static junit.framework.Assert.assertEquals;
17+
import static junit.framework.Assert.assertNotNull;
1718
import static junit.framework.Assert.assertTrue;
1819
import static org.mockito.Matchers.any;
20+
import static org.mockito.Matchers.anyString;
1921
import static org.mockito.Matchers.eq;
2022
import static org.mockito.Mockito.mock;
23+
import static org.mockito.Mockito.never;
2124
import static org.mockito.Mockito.times;
2225
import static org.mockito.Mockito.verify;
26+
import static org.mockito.Mockito.when;
2327

2428
@RunWith(RobolectricGradleTestRunner.class)
2529
@Config(constants = BuildConfig.class, sdk = 21)
@@ -79,6 +83,34 @@ public void testUnsubscribeWhenSubscribedToCallback() throws Exception {
7983
verify(unsubscribeMockCallback, times(1)).onUnsubscribe(parseQuery);
8084
}
8185

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+
82114
@Test
83115
public void testErrorWhenSubscribedToCallback() throws Exception {
84116
ParseQuery<ParseObject> parseQuery = new ParseQuery<>("test");

0 commit comments

Comments
 (0)