Skip to content

Commit

Permalink
Change ArgumentCaptor.forClass(null) to ArgumentCaptor.forClass(Foo.c…
Browse files Browse the repository at this point in the history
…lass)

The former will [soon throw NullPointerException][notnull].

[notnull]: mockito/mockito@fe1cb2d#diff-8d274a9bda2d871524d15bbfcd6272bd893a47e6b1a0b460d82a8845615f26daR31
  • Loading branch information
cpovirk authored and ejona86 committed Jan 9, 2023
1 parent cc53784 commit c0de130
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void parameterPropagation_base() {

transport.newStream(method, origHeaders, callOptions, tracers);

ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(RequestInfo.class);
verify(mockCreds).applyRequestMetadata(infoCaptor.capture(), same(mockExecutor),
any(CallCredentials.MetadataApplier.class));
RequestInfo info = infoCaptor.getValue();
Expand All @@ -169,7 +169,7 @@ public void parameterPropagation_overrideByCallOptions() {
callOptions.withAuthority("calloptions-authority").withExecutor(anotherExecutor),
tracers);

ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(RequestInfo.class);
verify(mockCreds).applyRequestMetadata(infoCaptor.capture(),
same(mockExecutor), any(CallCredentials.MetadataApplier.class));
RequestInfo info = infoCaptor.getValue();
Expand All @@ -189,7 +189,7 @@ public void parameterPropagation_transportSetSecurityLevel() {

transport.newStream(method, origHeaders, callOptions, tracers);

ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(RequestInfo.class);
verify(mockCreds).applyRequestMetadata(
infoCaptor.capture(), same(mockExecutor),
any(io.grpc.CallCredentials.MetadataApplier.class));
Expand All @@ -213,7 +213,7 @@ public void parameterPropagation_callOptionsSetAuthority() {
callOptions.withAuthority("calloptions-authority").withExecutor(anotherExecutor),
tracers);

ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(RequestInfo.class);
verify(mockCreds).applyRequestMetadata(
infoCaptor.capture(), same(mockExecutor),
any(io.grpc.CallCredentials.MetadataApplier.class));
Expand Down Expand Up @@ -333,7 +333,8 @@ public void applyMetadata_delayed() {
DelayedStream stream = (DelayedStream) transport.newStream(
method, origHeaders, callOptions, tracers);

ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor =
ArgumentCaptor.forClass(CallCredentials.MetadataApplier.class);
verify(mockCreds).applyRequestMetadata(any(RequestInfo.class),
same(mockExecutor), applierCaptor.capture());
verify(mockTransport, never()).newStream(
Expand All @@ -359,7 +360,8 @@ public void applyMetadata_delayed() {
@Test
public void delayedShutdown_shutdownShutdownNowThenApply() {
transport.newStream(method, origHeaders, callOptions, tracers);
ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor =
ArgumentCaptor.forClass(CallCredentials.MetadataApplier.class);
verify(mockCreds).applyRequestMetadata(any(RequestInfo.class),
same(mockExecutor), applierCaptor.capture());
transport.shutdown(Status.UNAVAILABLE);
Expand All @@ -380,7 +382,8 @@ public void delayedShutdown_shutdownShutdownNowThenApply() {
@Test
public void delayedShutdown_shutdownThenApplyThenShutdownNow() {
transport.newStream(method, origHeaders, callOptions, tracers);
ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor =
ArgumentCaptor.forClass(CallCredentials.MetadataApplier.class);
verify(mockCreds).applyRequestMetadata(any(RequestInfo.class),
same(mockExecutor), applierCaptor.capture());
transport.shutdown(Status.UNAVAILABLE);
Expand Down Expand Up @@ -408,7 +411,8 @@ public void delayedShutdown_shutdownMulti() {
transport.newStream(method, origHeaders, callOptions, tracers);
transport.newStream(method, origHeaders, callOptions, tracers);
transport.newStream(method, origHeaders, callOptions, tracers);
ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor =
ArgumentCaptor.forClass(CallCredentials.MetadataApplier.class);
verify(mockCreds, times(3)).applyRequestMetadata(any(RequestInfo.class),
same(mockExecutor), applierCaptor.capture());
applierCaptor.getAllValues().get(1).apply(headers);
Expand Down Expand Up @@ -436,7 +440,8 @@ public void fail_delayed() {
DelayedStream stream = (DelayedStream) transport.newStream(
method, origHeaders, callOptions, tracers);

ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor =
ArgumentCaptor.forClass(CallCredentials.MetadataApplier.class);
verify(mockCreds).applyRequestMetadata(any(RequestInfo.class),
same(mockExecutor), applierCaptor.capture());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ public void callOptionsPropagatedToTransport() {

@Test
public void methodInfoDeadlinePropagatedToStream() {
ArgumentCaptor<CallOptions> callOptionsCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<CallOptions> callOptionsCaptor = ArgumentCaptor.forClass(CallOptions.class);
CallOptions callOptions = baseCallOptions.withDeadline(Deadline.after(2000, SECONDS));

// Case: config Deadline expires later than CallOptions Deadline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public Result selectConfig(PickSubchannelArgs args) {
method,
CallOptions.DEFAULT);
configSelectingClientCall.start(callListener, new Metadata());
ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
verify(callListener).onClose(statusCaptor.capture(), any(Metadata.class));
assertThat(statusCaptor.getValue().getCode()).isEqualTo(Status.Code.DEADLINE_EXCEEDED);

Expand All @@ -157,7 +157,7 @@ public Result selectConfig(PickSubchannelArgs args) {
method,
CallOptions.DEFAULT);
configSelectingClientCall.start(callListener, new Metadata());
ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
verify(callListener).onClose(statusCaptor.capture(), any(Metadata.class));
// ... so it should be represented as an internal error to highlight the control plane bug.
assertThat(statusCaptor.getValue().getCode()).isEqualTo(Status.Code.INTERNAL);
Expand Down
12 changes: 8 additions & 4 deletions core/src/test/java/io/grpc/internal/DelayedClientCallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public void listenerEventsPropagated() {
callExecutor, fakeClock.getScheduledExecutorService(), Deadline.after(10, SECONDS));
delayedClientCall.start(listener, new Metadata());
callMeMaybe(delayedClientCall.setCall(mockRealCall));
ArgumentCaptor<Listener<Integer>> listenerCaptor = ArgumentCaptor.forClass(null);
@SuppressWarnings("unchecked")
ArgumentCaptor<Listener<Integer>> listenerCaptor = ArgumentCaptor.forClass(Listener.class);
verify(mockRealCall).start(listenerCaptor.capture(), any(Metadata.class));
Listener<Integer> realCallListener = listenerCaptor.getValue();
Metadata metadata = new Metadata();
Expand All @@ -133,7 +134,8 @@ public void setCallThenStart() {
callMeMaybe(delayedClientCall.setCall(mockRealCall));
delayedClientCall.start(listener, new Metadata());
delayedClientCall.request(1);
ArgumentCaptor<Listener<Integer>> listenerCaptor = ArgumentCaptor.forClass(null);
@SuppressWarnings("unchecked")
ArgumentCaptor<Listener<Integer>> listenerCaptor = ArgumentCaptor.forClass(Listener.class);
verify(mockRealCall).start(listenerCaptor.capture(), any(Metadata.class));
Listener<Integer> realCallListener = listenerCaptor.getValue();
verify(mockRealCall).request(1);
Expand All @@ -150,7 +152,8 @@ public void startThenSetCall() {
Runnable r = delayedClientCall.setCall(mockRealCall);
assertThat(r).isNotNull();
r.run();
ArgumentCaptor<Listener<Integer>> listenerCaptor = ArgumentCaptor.forClass(null);
@SuppressWarnings("unchecked")
ArgumentCaptor<Listener<Integer>> listenerCaptor = ArgumentCaptor.forClass(Listener.class);
verify(mockRealCall).start(listenerCaptor.capture(), any(Metadata.class));
Listener<Integer> realCallListener = listenerCaptor.getValue();
verify(mockRealCall).request(1);
Expand Down Expand Up @@ -185,7 +188,8 @@ public void setCallThenCancel() {
assertThat(r).isNotNull();
r.run();
delayedClientCall.cancel("cancel", new StatusException(Status.CANCELLED));
ArgumentCaptor<Listener<Integer>> listenerCaptor = ArgumentCaptor.forClass(null);
@SuppressWarnings("unchecked")
ArgumentCaptor<Listener<Integer>> listenerCaptor = ArgumentCaptor.forClass(Listener.class);
verify(mockRealCall).start(listenerCaptor.capture(), any(Metadata.class));
Listener<Integer> realCallListener = listenerCaptor.getValue();
verify(mockRealCall).request(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public void realTransportsHoldsOffIdleness() throws Exception {
call.start(mockCallListener, new Metadata());

// Verify that we have exited the idle mode
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
deliverResolutionResult();
Helper helper = helperCaptor.getValue();
Expand Down Expand Up @@ -373,7 +373,7 @@ public void enterIdleWhileRealTransportInProgress() {
call.start(mockCallListener, new Metadata());

// Verify that we have exited the idle mode
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
deliverResolutionResult();
Helper helper = helperCaptor.getValue();
Expand Down Expand Up @@ -412,7 +412,7 @@ public void enterIdleWhileRealTransportInProgress() {
public void updateSubchannelAddresses_newAddressConnects() {
ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
call.start(mockCallListener, new Metadata()); // Create LB
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
deliverResolutionResult();
Helper helper = helperCaptor.getValue();
Expand All @@ -436,7 +436,7 @@ public void updateSubchannelAddresses_newAddressConnects() {
public void updateSubchannelAddresses_existingAddressDoesNotConnect() {
ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
call.start(mockCallListener, new Metadata()); // Create LB
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
deliverResolutionResult();
Helper helper = helperCaptor.getValue();
Expand All @@ -461,7 +461,7 @@ public void oobTransportDoesNotAffectIdleness() {
call.start(mockCallListener, new Metadata());

// Verify that we have exited the idle mode
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
Helper helper = helperCaptor.getValue();
deliverResolutionResult();
Expand Down Expand Up @@ -510,7 +510,7 @@ public void oobTransportDoesNotAffectIdleness() {
public void updateOobChannelAddresses_newAddressConnects() {
ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
call.start(mockCallListener, new Metadata()); // Create LB
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
deliverResolutionResult();
Helper helper = helperCaptor.getValue();
Expand All @@ -534,7 +534,7 @@ public void updateOobChannelAddresses_newAddressConnects() {
public void updateOobChannelAddresses_existingAddressDoesNotConnect() {
ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
call.start(mockCallListener, new Metadata()); // Create LB
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
Helper helper = helperCaptor.getValue();
deliverResolutionResult();
Expand Down
29 changes: 16 additions & 13 deletions core/src/test/java/io/grpc/internal/ManagedChannelImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public void run() {

assertEquals(numExpectedTasks, timer.numPendingTasks());

ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
helper = helperCaptor.getValue();
}
Expand Down Expand Up @@ -402,7 +402,8 @@ public void createSubchannel_resolverOverrideAuthority() {
Subchannel subchannel =
createSubchannelSafely(helper, addressGroup, Attributes.EMPTY, subchannelStateListener);
requestConnectionSafely(helper, subchannel);
ArgumentCaptor<ClientTransportOptions> transportOptionCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<ClientTransportOptions> transportOptionCaptor =
ArgumentCaptor.forClass(ClientTransportOptions.class);
verify(mockTransportFactory)
.newClientTransport(
any(SocketAddress.class), transportOptionCaptor.capture(), any(ChannelLogger.class));
Expand All @@ -427,7 +428,8 @@ public void createSubchannel_channelBuilderOverrideAuthority() {
final Subchannel subchannel =
createSubchannelSafely(helper, addressGroup, Attributes.EMPTY, subchannelStateListener);
requestConnectionSafely(helper, subchannel);
ArgumentCaptor<ClientTransportOptions> transportOptionCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<ClientTransportOptions> transportOptionCaptor =
ArgumentCaptor.forClass(ClientTransportOptions.class);
verify(mockTransportFactory)
.newClientTransport(
any(SocketAddress.class), transportOptionCaptor.capture(), any(ChannelLogger.class));
Expand Down Expand Up @@ -494,7 +496,7 @@ channelBuilder, mockTransportFactory, new FakeBackoffPolicyProvider(),
ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
call.start(mockCallListener, headers);

ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
helper = helperCaptor.getValue();
// Make the transport available
Expand All @@ -520,7 +522,7 @@ channelBuilder, mockTransportFactory, new FakeBackoffPolicyProvider(),
updateBalancingStateSafely(helper, READY, mockPicker);
executor.runDueTasks();

ArgumentCaptor<CallOptions> callOptionsCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<CallOptions> callOptionsCaptor = ArgumentCaptor.forClass(CallOptions.class);
verify(mockTransport).newStream(
same(method), same(headers), callOptionsCaptor.capture(),
ArgumentMatchers.<ClientStreamTracer[]>any());
Expand Down Expand Up @@ -576,7 +578,7 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
call.start(mockCallListener, headers);

ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
helper = helperCaptor.getValue();
// Make the transport available
Expand All @@ -599,7 +601,7 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
updateBalancingStateSafely(helper, READY, mockPicker);
executor.runDueTasks();

ArgumentCaptor<CallOptions> callOptionsCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<CallOptions> callOptionsCaptor = ArgumentCaptor.forClass(CallOptions.class);
verify(mockTransport).newStream(
same(method), same(headers), callOptionsCaptor.capture(),
ArgumentMatchers.<ClientStreamTracer[]>any());
Expand Down Expand Up @@ -2270,9 +2272,10 @@ public ClientStream answer(InvocationOnMock in) throws Throwable {
.thenReturn(PickResult.withSubchannel(subchannel));
updateBalancingStateSafely(helper, READY, mockPicker);
executor.runDueTasks();
ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Executor> executorArgumentCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(RequestInfo.class);
ArgumentCaptor<Executor> executorArgumentCaptor = ArgumentCaptor.forClass(Executor.class);
ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor =
ArgumentCaptor.forClass(CallCredentials.MetadataApplier.class);
verify(creds).applyRequestMetadata(infoCaptor.capture(),
executorArgumentCaptor.capture(), applierCaptor.capture());
assertSame(offloadExecutor,
Expand Down Expand Up @@ -2435,7 +2438,7 @@ public void getState_withRequestConnect() {

// call getState() with requestConnection = true
assertEquals(IDLE, channel.getState(true));
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
helper = helperCaptor.getValue();

Expand Down Expand Up @@ -2732,7 +2735,7 @@ private void verifyPanicMode(Throwable cause) {

private void verifyCallListenerClosed(
ClientCall.Listener<Integer> listener, Status.Code code, Throwable cause) {
ArgumentCaptor<Status> captor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Status> captor = ArgumentCaptor.forClass(Status.class);
verify(listener).onClose(captor.capture(), any(Metadata.class));
Status rpcStatus = captor.getValue();
assertEquals(code, rpcStatus.getCode());
Expand Down Expand Up @@ -3343,7 +3346,7 @@ public void channelTracing_oobChannelCreationEvents() throws Exception {
public void channelsAndSubchannels_instrumented_state() throws Exception {
createChannel();

ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
helper = helperCaptor.getValue();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void run() {
}

assertEquals(numExpectedTasks, timer.numPendingTasks());
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ public int hashCode() {
@Test
public void transientFailureOnInitialResolutionError() {
gracefulSwitchLb.handleNameResolutionError(Status.DATA_LOSS);
ArgumentCaptor<SubchannelPicker> pickerCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<SubchannelPicker> pickerCaptor = ArgumentCaptor.forClass(SubchannelPicker.class);
verify(mockHelper).updateBalancingState(eq(TRANSIENT_FAILURE), pickerCaptor.capture());
SubchannelPicker picker = pickerCaptor.getValue();
assertThat(picker.pickSubchannel(mock(PickSubchannelArgs.class)).getStatus().getCode())
Expand Down
Loading

0 comments on commit c0de130

Please sign in to comment.