From ad0893737e6af39f131bb48869003c6386019aea Mon Sep 17 00:00:00 2001 From: Tim van der Lippe Date: Wed, 29 May 2019 10:29:29 -0700 Subject: [PATCH] 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. --- .../test/java/io/grpc/ClientInterceptorsTest.java | 13 +++++++------ api/src/test/java/io/grpc/NameResolverTest.java | 3 +-- .../test/java/io/grpc/ServerInterceptorsTest.java | 9 ++++++--- .../io/grpc/cronet/CronetClientTransportTest.java | 2 +- .../io/grpc/grpclb/CachedSubchannelPoolTest.java | 2 +- .../src/test/java/io/grpc/stub/StubConfigTest.java | 5 +++-- .../grpc/okhttp/OkHttpProtocolNegotiatorTest.java | 6 +++--- 7 files changed, 22 insertions(+), 18 deletions(-) diff --git a/api/src/test/java/io/grpc/ClientInterceptorsTest.java b/api/src/test/java/io/grpc/ClientInterceptorsTest.java index ee98ac8679..df5e800758 100644 --- a/api/src/test/java/io/grpc/ClientInterceptorsTest.java +++ b/api/src/test/java/io/grpc/ClientInterceptorsTest.java @@ -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.>any(), any(CallOptions.class))) + ArgumentMatchers.>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.any()); + verify(interceptor) + .interceptCall(same(method), same(callOptions), ArgumentMatchers.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.any()); + .interceptCall(same(method), same(callOptions), ArgumentMatchers.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.any()); + verify(interceptor) + .interceptCall(same(method), same(initialCallOptions), ArgumentMatchers.any()); verify(channel).newCall(same(method), same(newCallOptions)); } diff --git a/api/src/test/java/io/grpc/NameResolverTest.java b/api/src/test/java/io/grpc/NameResolverTest.java index 668208f990..b641fcac68 100644 --- a/api/src/test/java/io/grpc/NameResolverTest.java +++ b/api/src/test/java/io/grpc/NameResolverTest.java @@ -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; diff --git a/api/src/test/java/io/grpc/ServerInterceptorsTest.java b/api/src/test/java/io/grpc/ServerInterceptorsTest.java index e6905cd73b..4d17338119 100644 --- a/api/src/test/java/io/grpc/ServerInterceptorsTest.java +++ b/api/src/test/java/io/grpc/ServerInterceptorsTest.java @@ -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.>any(), Mockito.any())) + Mockito.when( + handler.startCall( + ArgumentMatchers.>any(), + ArgumentMatchers.any())) .thenReturn(listener); serviceDefinition = ServerServiceDefinition.builder(new ServiceDescriptor("basic", flowMethod)) @@ -438,7 +441,7 @@ public class ServerInterceptorsTest { } private ServerCallHandler anyCallHandler() { - return Mockito.any(); + return ArgumentMatchers.any(); } private static class NoopInterceptor implements ServerInterceptor { diff --git a/cronet/src/test/java/io/grpc/cronet/CronetClientTransportTest.java b/cronet/src/test/java/io/grpc/cronet/CronetClientTransportTest.java index dbfa4af2fa..845ba8db0e 100644 --- a/cronet/src/test/java/io/grpc/cronet/CronetClientTransportTest.java +++ b/cronet/src/test/java/io/grpc/cronet/CronetClientTransportTest.java @@ -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; diff --git a/grpclb/src/test/java/io/grpc/grpclb/CachedSubchannelPoolTest.java b/grpclb/src/test/java/io/grpc/grpclb/CachedSubchannelPoolTest.java index ead3ab1d1e..a376aa5a67 100644 --- a/grpclb/src/test/java/io/grpc/grpclb/CachedSubchannelPoolTest.java +++ b/grpclb/src/test/java/io/grpc/grpclb/CachedSubchannelPoolTest.java @@ -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; diff --git a/interop-testing/src/test/java/io/grpc/stub/StubConfigTest.java b/interop-testing/src/test/java/io/grpc/stub/StubConfigTest.java index e5f05e8690..ab576d0c07 100644 --- a/interop-testing/src/test/java/io/grpc/stub/StubConfigTest.java +++ b/interop-testing/src/test/java/io/grpc/stub/StubConfigTest.java @@ -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 call = new NoopClientCall<>(); when(channel.newCall( - Mockito.>any(), any(CallOptions.class))) + ArgumentMatchers.>any(), + any(CallOptions.class))) .thenReturn(call); } diff --git a/okhttp/src/test/java/io/grpc/okhttp/OkHttpProtocolNegotiatorTest.java b/okhttp/src/test/java/io/grpc/okhttp/OkHttpProtocolNegotiatorTest.java index 67d6782588..370b0f6add 100644 --- a/okhttp/src/test/java/io/grpc/okhttp/OkHttpProtocolNegotiatorTest.java +++ b/okhttp/src/test/java/io/grpc/okhttp/OkHttpProtocolNegotiatorTest.java @@ -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.any())).thenReturn("h2"); + when(platform.getSelectedProtocol(ArgumentMatchers.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.any())).thenReturn("grpc-exp"); + when(platform.getSelectedProtocol(ArgumentMatchers.any())).thenReturn("grpc-exp"); OkHttpProtocolNegotiator negotiator = new OkHttpProtocolNegotiator(platform); String actual =