Skip to content

Commit dbd89d6

Browse files
committed
test(pubsub): fix mock stubbing to prevent NPEs in StreamingSubscriberConnectionTest
Stub mock callables to return pending SettableApiFuture and default ClientStream to avoid NullPointerException during connection startup and operation in tests. TAG=agy CONV=de3ef09d-a26c-4190-b578-4c993f807bdc
1 parent d58ab04 commit dbd89d6

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

java-pubsub/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/v1/StreamingSubscriberConnectionTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public class StreamingSubscriberConnectionTest {
104104
private static final Duration MAX_ACK_EXTENSION_PERIOD = Duration.ofMinutes(60);
105105

106106
@Before
107-
public void setUp() {
107+
public void setUp() throws Exception {
108108
systemExecutor = new FakeScheduledExecutorService();
109109
executor = new FakeScheduledExecutorService();
110110
clock = systemExecutor.getClock();
@@ -117,6 +117,15 @@ public void setUp() {
117117
when(mockSubscriberStub.streamingPullCallable()).thenReturn(mockStreamingCallable);
118118
when(mockSubscriberStub.acknowledgeCallable()).thenReturn(mockAcknowledgeCallable);
119119
when(mockSubscriberStub.modifyAckDeadlineCallable()).thenReturn(mockModifyAckDeadlineCallable);
120+
121+
ClientStream<StreamingPullRequest> defaultMockClientStream =
122+
mock(ClientStream.class, withSettings().withoutAnnotations());
123+
when(mockStreamingCallable.splitCall(any(), any())).thenReturn(defaultMockClientStream);
124+
125+
when(mockAcknowledgeCallable.futureCall(any()))
126+
.thenAnswer(invocation -> SettableApiFuture.create());
127+
when(mockModifyAckDeadlineCallable.futureCall(any()))
128+
.thenAnswer(invocation -> SettableApiFuture.create());
120129
}
121130

122131
@After

0 commit comments

Comments
 (0)