Prepare for JUnit 4.13

It deprecates ExpectedException and Assert.assertThat(T, org.hamcrest.Matcher).
Without Java 8 we don't want to migrate away from ExpectedException at
this time. We tend to prefer Truth over Hamcrest, so I swapped the one
instance of Assert.assertThat() to use Truth. With this change we get a
warning-less build with JUnit 4.13. We don't yet upgrade because we
still need to support JUnit 4.12 for some use-cases, but will be able to
upgrade to 4.13 soon when they upgrade.
This commit is contained in:
Eric Anderson 2020-09-28 13:19:56 -07:00 committed by Eric Anderson
parent 2adeff56fe
commit 4c1bab9ed5
34 changed files with 37 additions and 5 deletions

View File

@ -16,15 +16,14 @@
package io.grpc; package io.grpc;
import static com.google.common.truth.Truth.assertThat;
import static io.grpc.Contexts.interceptCall; import static io.grpc.Contexts.interceptCall;
import static io.grpc.Contexts.statusFromCancelled; import static io.grpc.Contexts.statusFromCancelled;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -240,7 +239,7 @@ public class ContextsTest {
executorService.command.run(); executorService.command.run();
assertTrue(cancellableContext.isCancelled()); assertTrue(cancellableContext.isCancelled());
assertThat(cancellableContext.cancellationCause(), instanceOf(TimeoutException.class)); assertThat(cancellableContext.cancellationCause()).isInstanceOf(TimeoutException.class);
Status status = statusFromCancelled(cancellableContext); Status status = statusFromCancelled(cancellableContext);
assertNotNull(status); assertNotNull(status);

View File

@ -49,6 +49,7 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class MetadataTest { public class MetadataTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none(); @Rule public final ExpectedException thrown = ExpectedException.none();
private static final Metadata.BinaryMarshaller<Fish> FISH_MARSHALLER = private static final Metadata.BinaryMarshaller<Fish> FISH_MARSHALLER =

View File

@ -37,6 +37,7 @@ import org.junit.runners.JUnit4;
*/ */
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class MethodDescriptorTest { public class MethodDescriptorTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();

View File

@ -56,6 +56,7 @@ public class ServerInterceptorsTest {
@Rule @Rule
public final MockitoRule mocks = MockitoJUnit.rule(); public final MockitoRule mocks = MockitoJUnit.rule();
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();

View File

@ -52,6 +52,7 @@ public class ServerServiceDefinitionTest {
= ServerMethodDefinition.create(method1, methodHandler1); = ServerMethodDefinition.create(method1, methodHandler1);
private ServerMethodDefinition<String, Integer> methodDef2 private ServerMethodDefinition<String, Integer> methodDef2
= ServerMethodDefinition.create(method2, methodHandler2); = ServerMethodDefinition.create(method2, methodHandler2);
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();

View File

@ -36,6 +36,7 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class ServiceDescriptorTest { public class ServiceDescriptorTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();

View File

@ -75,6 +75,7 @@ import org.mockito.stubbing.Answer;
public class AbstractClientStreamTest { public class AbstractClientStreamTest {
@Rule public final MockitoRule mocks = MockitoJUnit.rule(); @Rule public final MockitoRule mocks = MockitoJUnit.rule();
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none(); @Rule public final ExpectedException thrown = ExpectedException.none();
private final StatsTraceContext statsTraceCtx = StatsTraceContext.NOOP; private final StatsTraceContext statsTraceCtx = StatsTraceContext.NOOP;

View File

@ -57,6 +57,7 @@ public class AbstractServerStreamTest {
private static final int TIMEOUT_MS = 1000; private static final int TIMEOUT_MS = 1000;
private static final int MAX_MESSAGE_SIZE = 100; private static final int MAX_MESSAGE_SIZE = 100;
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none(); @Rule public final ExpectedException thrown = ExpectedException.none();
private final WritableBufferAllocator allocator = new WritableBufferAllocator() { private final WritableBufferAllocator allocator = new WritableBufferAllocator() {

View File

@ -224,6 +224,7 @@ public abstract class AbstractTransportTest {
} }
})); }));
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();

View File

@ -38,6 +38,7 @@ import org.junit.runners.JUnit4;
*/ */
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class ConnectivityStateManagerTest { public class ConnectivityStateManagerTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();

View File

@ -98,6 +98,7 @@ public class DnsNameResolverTest {
@Rule public final TestRule globalTimeout = new DisableOnDebug(Timeout.seconds(10)); @Rule public final TestRule globalTimeout = new DisableOnDebug(Timeout.seconds(10));
@Rule public final MockitoRule mocks = MockitoJUnit.rule(); @Rule public final MockitoRule mocks = MockitoJUnit.rule();
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none(); @Rule public final ExpectedException thrown = ExpectedException.none();
private final Map<String, ?> serviceConfig = new LinkedHashMap<>(); private final Map<String, ?> serviceConfig = new LinkedHashMap<>();

View File

@ -44,6 +44,7 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class GrpcUtilTest { public class GrpcUtilTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none(); @Rule public final ExpectedException thrown = ExpectedException.none();
@Test @Test

View File

@ -77,6 +77,7 @@ import org.mockito.junit.MockitoRule;
public class InternalSubchannelTest { public class InternalSubchannelTest {
@Rule @Rule
public final MockitoRule mocks = MockitoJUnit.rule(); public final MockitoRule mocks = MockitoJUnit.rule();
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();

View File

@ -35,6 +35,7 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class JsonParserTest { public class JsonParserTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();
@ -122,4 +123,4 @@ public class JsonParserTest {
assertEquals(expected, JsonParser.parse("{\"hi\": 2}")); assertEquals(expected, JsonParser.parse("{\"hi\": 2}"));
} }
} }

View File

@ -80,6 +80,7 @@ public class ManagedChannelImplBuilderTest {
}; };
@Rule public final MockitoRule mocks = MockitoJUnit.rule(); @Rule public final MockitoRule mocks = MockitoJUnit.rule();
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none(); @Rule public final ExpectedException thrown = ExpectedException.none();
@Rule public final GrpcCleanupRule grpcCleanupRule = new GrpcCleanupRule(); @Rule public final GrpcCleanupRule grpcCleanupRule = new GrpcCleanupRule();

View File

@ -41,6 +41,7 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class ManagedChannelServiceConfigTest { public class ManagedChannelServiceConfigTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();
@ -224,4 +225,4 @@ public class ManagedChannelServiceConfigTest {
private static Map<String, Object> parseConfig(String json) throws Exception { private static Map<String, Object> parseConfig(String json) throws Exception {
return (Map<String, Object>) JsonParser.parse(json); return (Map<String, Object>) JsonParser.parse(json);
} }
} }

View File

@ -337,6 +337,7 @@ public class MessageDeframerTest {
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public static class SizeEnforcingInputStreamTests { public static class SizeEnforcingInputStreamTests {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();

View File

@ -59,6 +59,7 @@ import org.mockito.MockitoAnnotations;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class ServerCallImplTest { public class ServerCallImplTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none(); @Rule public final ExpectedException thrown = ExpectedException.none();
@Mock private ServerStream stream; @Mock private ServerStream stream;
@Mock private ServerCall.Listener<Long> callListener; @Mock private ServerCall.Listener<Long> callListener;

View File

@ -139,6 +139,7 @@ public class ServerImplTest {
}; };
private static final String AUTHORITY = "some_authority"; private static final String AUTHORITY = "some_authority";
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none(); @Rule public final ExpectedException thrown = ExpectedException.none();
@BeforeClass @BeforeClass

View File

@ -62,6 +62,7 @@ import org.mockito.InOrder;
*/ */
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class GracefulSwitchLoadBalancerTest { public class GracefulSwitchLoadBalancerTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();

View File

@ -41,6 +41,7 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class NettyChannelBuilderTest { public class NettyChannelBuilderTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none(); @Rule public final ExpectedException thrown = ExpectedException.none();
private final SslContext noSslContext = null; private final SslContext noSslContext = null;

View File

@ -40,6 +40,7 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class NettyServerBuilderTest { public class NettyServerBuilderTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none(); @Rule public final ExpectedException thrown = ExpectedException.none();
private NettyServerBuilder builder = NettyServerBuilder.forPort(8080); private NettyServerBuilder builder = NettyServerBuilder.forPort(8080);

View File

@ -122,6 +122,7 @@ public class ProtocolNegotiatorsTest {
private static final int TIMEOUT_SECONDS = 60; private static final int TIMEOUT_SECONDS = 60;
@Rule public final TestRule globalTimeout = new DisableOnDebug(Timeout.seconds(TIMEOUT_SECONDS)); @Rule public final TestRule globalTimeout = new DisableOnDebug(Timeout.seconds(TIMEOUT_SECONDS));
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none(); @Rule public final ExpectedException thrown = ExpectedException.none();
private final EventLoopGroup group = new DefaultEventLoop(); private final EventLoopGroup group = new DefaultEventLoop();

View File

@ -47,6 +47,7 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class OkHttpChannelBuilderTest { public class OkHttpChannelBuilderTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none(); @Rule public final ExpectedException thrown = ExpectedException.none();
@Rule public final GrpcCleanupRule grpcCleanupRule = new GrpcCleanupRule(); @Rule public final GrpcCleanupRule grpcCleanupRule = new GrpcCleanupRule();

View File

@ -49,6 +49,7 @@ import org.mockito.ArgumentMatchers;
*/ */
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class OkHttpProtocolNegotiatorTest { public class OkHttpProtocolNegotiatorTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none(); @Rule public final ExpectedException thrown = ExpectedException.none();
private final SSLSocket sock = mock(SSLSocket.class); private final SSLSocket sock = mock(SSLSocket.class);

View File

@ -37,6 +37,7 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class UtilsTest { public class UtilsTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();

View File

@ -51,6 +51,7 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class ProtoLiteUtilsTest { public class ProtoLiteUtilsTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none(); @Rule public final ExpectedException thrown = ExpectedException.none();
private Marshaller<Type> marshaller = ProtoLiteUtils.marshaller(Type.getDefaultInstance()); private Marshaller<Type> marshaller = ProtoLiteUtils.marshaller(Type.getDefaultInstance());

View File

@ -51,6 +51,7 @@ import org.mockito.InOrder;
public class GrpcCleanupRuleTest { public class GrpcCleanupRuleTest {
public static final FakeClock fakeClock = new FakeClock(); public static final FakeClock fakeClock = new FakeClock();
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();

View File

@ -40,6 +40,7 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class BootstrapperTest { public class BootstrapperTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public ExpectedException thrown = ExpectedException.none(); @Rule public ExpectedException thrown = ExpectedException.none();
@Test @Test

View File

@ -34,6 +34,7 @@ import org.junit.runners.JUnit4;
/** Tests for {@link PriorityLoadBalancerProvider}. */ /** Tests for {@link PriorityLoadBalancerProvider}. */
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class PriorityLoadBalancerProviderTest { public class PriorityLoadBalancerProviderTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none(); @Rule public final ExpectedException thrown = ExpectedException.none();
@SuppressWarnings("ExpectedExceptionChecker") @SuppressWarnings("ExpectedExceptionChecker")

View File

@ -42,6 +42,7 @@ import org.mockito.junit.MockitoRule;
*/ */
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class WeightedRandomPickerTest { public class WeightedRandomPickerTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();

View File

@ -176,6 +176,7 @@ public class XdsClientImplTest {
@Rule @Rule
public final GrpcCleanupRule cleanupRule = new GrpcCleanupRule(); public final GrpcCleanupRule cleanupRule = new GrpcCleanupRule();
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();

View File

@ -34,6 +34,7 @@ import org.junit.runners.JUnit4;
*/ */
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class XdsClientTest { public class XdsClientTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();

View File

@ -44,6 +44,7 @@ import org.mockito.junit.MockitoRule;
/** Unit tests for constructor of CEL-based Authorization Engine. */ /** Unit tests for constructor of CEL-based Authorization Engine. */
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class AuthzEngineTest { public class AuthzEngineTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule @Rule
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();