Migrate org.mockito.Matchers#any* to org.mockito.ArgumentMatchers

The former is deprecated and replaced by the latter in Mockito 2.
However, there is a functional difference: ArgumentMatchers will reject
`null` and check the type if the matcher specified a type (e.g.
`any(Class)` or `anyInt()`). `any()` will remain to accept anything.
This commit is contained in:
Tim van der Lippe 2019-05-29 10:29:29 -07:00 committed by Eric Anderson
parent 4e5e19f6fd
commit ad0893737e
7 changed files with 22 additions and 18 deletions

View File

@ -46,8 +46,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
@ -70,7 +70,7 @@ public class ClientInterceptorsTest {
*/
@Before public void setUp() {
when(channel.newCall(
Mockito.<MethodDescriptor<String, Integer>>any(), any(CallOptions.class)))
ArgumentMatchers.<MethodDescriptor<String, Integer>>any(), any(CallOptions.class)))
.thenReturn(call);
}
@ -103,13 +103,14 @@ public class ClientInterceptorsTest {
// First call
assertSame(call, intercepted.newCall(method, callOptions));
verify(channel).newCall(same(method), same(callOptions));
verify(interceptor).interceptCall(same(method), same(callOptions), Mockito.<Channel>any());
verify(interceptor)
.interceptCall(same(method), same(callOptions), ArgumentMatchers.<Channel>any());
verifyNoMoreInteractions(channel, interceptor);
// Second call
assertSame(call, intercepted.newCall(method, callOptions));
verify(channel, times(2)).newCall(same(method), same(callOptions));
verify(interceptor, times(2))
.interceptCall(same(method), same(callOptions), Mockito.<Channel>any());
.interceptCall(same(method), same(callOptions), ArgumentMatchers.<Channel>any());
verifyNoMoreInteractions(channel, interceptor);
}
@ -233,8 +234,8 @@ public class ClientInterceptorsTest {
}));
Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
intercepted.newCall(method, initialCallOptions);
verify(interceptor).interceptCall(
same(method), same(initialCallOptions), Mockito.<Channel>any());
verify(interceptor)
.interceptCall(same(method), same(initialCallOptions), ArgumentMatchers.<Channel>any());
verify(channel).newCall(same(method), same(newCallOptions));
}

View File

@ -17,7 +17,7 @@
package io.grpc;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.any;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.same;
import static org.mockito.Mockito.verify;
@ -25,7 +25,6 @@ import static org.mockito.Mockito.when;
import io.grpc.NameResolver.ConfigOrError;
import io.grpc.NameResolver.ServiceConfigParser;
import io.grpc.SynchronizationContext;
import java.lang.Thread.UncaughtExceptionHandler;
import java.net.URI;
import java.util.Collections;

View File

@ -44,6 +44,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
@ -88,8 +89,10 @@ public class ServerInterceptorsTest {
.setResponseMarshaller(responseMarshaller)
.build();
Mockito.when(handler.startCall(
Mockito.<ServerCall<String, Integer>>any(), Mockito.<Metadata>any()))
Mockito.when(
handler.startCall(
ArgumentMatchers.<ServerCall<String, Integer>>any(),
ArgumentMatchers.<Metadata>any()))
.thenReturn(listener);
serviceDefinition = ServerServiceDefinition.builder(new ServiceDescriptor("basic", flowMethod))
@ -438,7 +441,7 @@ public class ServerInterceptorsTest {
}
private ServerCallHandler<String, Integer> anyCallHandler() {
return Mockito.any();
return ArgumentMatchers.any();
}
private static class NoopInterceptor implements ServerInterceptor {

View File

@ -18,7 +18,7 @@ package io.grpc.cronet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

View File

@ -19,8 +19,8 @@ package io.grpc.grpclb;
import static com.google.common.truth.Truth.assertThat;
import static io.grpc.grpclb.CachedSubchannelPool.SHUTDOWN_TIMEOUT_MS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.atMost;
import static org.mockito.Mockito.doAnswer;

View File

@ -38,8 +38,8 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
/**
@ -62,7 +62,8 @@ public class StubConfigTest {
ClientCall<SimpleRequest, SimpleResponse> call =
new NoopClientCall<>();
when(channel.newCall(
Mockito.<MethodDescriptor<SimpleRequest, SimpleResponse>>any(), any(CallOptions.class)))
ArgumentMatchers.<MethodDescriptor<SimpleRequest, SimpleResponse>>any(),
any(CallOptions.class)))
.thenReturn(call);
}

View File

@ -41,7 +41,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;
import org.mockito.ArgumentMatchers;
/**
* Tests for {@link OkHttpProtocolNegotiator}.
@ -135,7 +135,7 @@ public class OkHttpProtocolNegotiatorTest {
@Test
public void negotiate_success() throws Exception {
when(platform.getSelectedProtocol(Mockito.<SSLSocket>any())).thenReturn("h2");
when(platform.getSelectedProtocol(ArgumentMatchers.<SSLSocket>any())).thenReturn("h2");
OkHttpProtocolNegotiator negotiator = new OkHttpProtocolNegotiator(platform);
String actual = negotiator.negotiate(sock, "hostname", ImmutableList.of(Protocol.HTTP_2));
@ -153,7 +153,7 @@ public class OkHttpProtocolNegotiatorTest {
// The main usefulness of the test is for future changes to
// OkHttpProtocolNegotiator, where we can catch any change that would affect
// grpc-exp preference.
when(platform.getSelectedProtocol(Mockito.<SSLSocket>any())).thenReturn("grpc-exp");
when(platform.getSelectedProtocol(ArgumentMatchers.<SSLSocket>any())).thenReturn("grpc-exp");
OkHttpProtocolNegotiator negotiator = new OkHttpProtocolNegotiator(platform);
String actual =