From bb4a7b71e92a69e5f4761b546f5e00068b5ae8f4 Mon Sep 17 00:00:00 2001 From: Rahul Kesharwani Date: Thu, 23 Jan 2020 17:26:53 +0530 Subject: [PATCH 1/2] test: cleaning up deprecated usages removed deprecated `ExpectedException.none()` usage across unit test cases. chore: address feedback comments --- .../api/gax/grpc/GrpcCallContextTest.java | 32 +++++--- ...GrpcDirectServerStreamingCallableTest.java | 16 ++-- .../grpc/GrpcDirectStreamingCallableTest.java | 4 - .../gax/grpc/GrpcTransportDescriptorTest.java | 4 - .../grpc/ProtoOperationTransformersTest.java | 41 ++++++---- .../com/google/api/gax/grpc/SettingsTest.java | 4 - .../ApiMessageOperationTransformersTest.java | 39 ++++++---- .../gax/httpjson/HttpJsonCallContextTest.java | 35 ++++++--- .../google/api/gax/httpjson/RetryingTest.java | 67 +++++++++-------- .../gax/batching/ThresholdBatcherTest.java | 26 +++---- .../google/api/gax/rpc/ApiExceptionsTest.java | 35 ++++++--- .../api/gax/rpc/BatchedRequestIssuerTest.java | 74 ++++++++++--------- .../google/api/gax/rpc/CancellationTest.java | 31 ++++---- .../api/gax/rpc/FirstElementCallableTest.java | 4 - .../com/google/api/gax/rpc/RetryingTest.java | 66 ++++++++++------- .../api/gax/rpc/SpoolingCallableTest.java | 13 ++-- .../google/api/gax/rpc/UnaryCallableTest.java | 3 - 17 files changed, 283 insertions(+), 211 deletions(-) diff --git a/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcCallContextTest.java b/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcCallContextTest.java index 06b2e4985..69c938f7d 100644 --- a/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcCallContextTest.java +++ b/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcCallContextTest.java @@ -46,9 +46,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.junit.Rule; +import org.junit.Assert; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.mockito.Mockito; @@ -56,12 +55,17 @@ @RunWith(JUnit4.class) public class GrpcCallContextTest { - @Rule public ExpectedException thrown = ExpectedException.none(); @Test public void testNullToSelfWrongType() { - thrown.expect(IllegalArgumentException.class); - GrpcCallContext.createDefault().nullToSelf(FakeCallContext.createDefault()); + try { + GrpcCallContext.createDefault().nullToSelf(FakeCallContext.createDefault()); + Assert.fail("GrpcCallContext should have thrown an exception"); + } catch (IllegalArgumentException expected) { + Truth.assertThat(expected) + .hasMessageThat() + .contains("context must be an instance of GrpcCallContext"); + } } @Test @@ -83,15 +87,25 @@ public void testWithTransportChannel() { @Test public void testWithTransportChannelWrongType() { - thrown.expect(IllegalArgumentException.class); FakeChannel channel = new FakeChannel(); - GrpcCallContext.createDefault().withTransportChannel(FakeTransportChannel.create(channel)); + try { + GrpcCallContext.createDefault().withTransportChannel(FakeTransportChannel.create(channel)); + Assert.fail("GrpcCallContext should have thrown an exception"); + } catch (IllegalArgumentException expected) { + Truth.assertThat(expected).hasMessageThat().contains("Expected GrpcTransportChannel"); + } } @Test public void testMergeWrongType() { - thrown.expect(IllegalArgumentException.class); - GrpcCallContext.createDefault().merge(FakeCallContext.createDefault()); + try { + GrpcCallContext.createDefault().merge(FakeCallContext.createDefault()); + Assert.fail("GrpcCallContext should have thrown an exception"); + } catch (IllegalArgumentException expected) { + Truth.assertThat(expected) + .hasMessageThat() + .contains("context must be an instance of " + "GrpcCallContext"); + } } @Test diff --git a/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcDirectServerStreamingCallableTest.java b/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcDirectServerStreamingCallableTest.java index 2d78ffe2d..fa5a41fbd 100644 --- a/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcDirectServerStreamingCallableTest.java +++ b/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcDirectServerStreamingCallableTest.java @@ -58,10 +58,9 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.junit.After; +import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -79,8 +78,6 @@ public class GrpcDirectServerStreamingCallableTest { private ServerStreamingCallSettings streamingCallSettings; private ServerStreamingCallable streamingCallable; - @Rule public ExpectedException thrown = ExpectedException.none(); - @Before public void setUp() throws InstantiationException, IllegalAccessException, IOException { String serverName = "fakeservice"; @@ -121,9 +118,14 @@ public void testBadContext() { CountDownLatch latch = new CountDownLatch(1); MoneyObserver observer = new MoneyObserver(true, latch); - - thrown.expect(IllegalArgumentException.class); - streamingCallable.call(DEFAULT_REQUEST, observer); + try { + streamingCallable.call(DEFAULT_REQUEST, observer); + Assert.fail("Callable should have thrown an exception"); + } catch (IllegalArgumentException expected) { + Truth.assertThat(expected) + .hasMessageThat() + .contains("context must be an instance of GrpcCallContext"); + } } @Test diff --git a/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcDirectStreamingCallableTest.java b/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcDirectStreamingCallableTest.java index f5b132038..733f4d02d 100644 --- a/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcDirectStreamingCallableTest.java +++ b/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcDirectStreamingCallableTest.java @@ -55,9 +55,7 @@ import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -68,8 +66,6 @@ public class GrpcDirectStreamingCallableTest { private FakeServiceImpl serviceImpl; private ClientContext clientContext; - @Rule public ExpectedException thrown = ExpectedException.none(); - @Before public void setUp() throws InstantiationException, IllegalAccessException, IOException { String serverName = "fakeservice"; diff --git a/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcTransportDescriptorTest.java b/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcTransportDescriptorTest.java index f79ca2298..9cd1aaa89 100644 --- a/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcTransportDescriptorTest.java +++ b/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcTransportDescriptorTest.java @@ -48,9 +48,7 @@ import io.grpc.StatusException; import io.grpc.StatusRuntimeException; import java.util.Collections; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -59,8 +57,6 @@ public class GrpcTransportDescriptorTest { private static boolean NOT_RETRYABLE = false; private static boolean IS_RETRYABLE = true; - @Rule public ExpectedException thrown = ExpectedException.none(); - @Test public void translateException_StatusException_noRetry() throws Exception { Throwable originalException = new StatusException(Status.INVALID_ARGUMENT); diff --git a/gax-grpc/src/test/java/com/google/api/gax/grpc/ProtoOperationTransformersTest.java b/gax-grpc/src/test/java/com/google/api/gax/grpc/ProtoOperationTransformersTest.java index 12ede0a38..d05bf3b65 100644 --- a/gax-grpc/src/test/java/com/google/api/gax/grpc/ProtoOperationTransformersTest.java +++ b/gax-grpc/src/test/java/com/google/api/gax/grpc/ProtoOperationTransformersTest.java @@ -29,27 +29,26 @@ */ package com.google.api.gax.grpc; +import static com.google.common.truth.Truth.assertThat; + import com.google.api.gax.grpc.ProtoOperationTransformers.MetadataTransformer; import com.google.api.gax.grpc.ProtoOperationTransformers.ResponseTransformer; import com.google.api.gax.longrunning.OperationSnapshot; import com.google.api.gax.rpc.ApiException; import com.google.api.gax.rpc.UnavailableException; -import com.google.common.truth.Truth; import com.google.longrunning.Operation; import com.google.protobuf.Any; import com.google.rpc.Status; import com.google.type.Color; import com.google.type.Money; import io.grpc.Status.Code; -import org.junit.Rule; +import org.junit.Assert; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) public class ProtoOperationTransformersTest { - @Rule public ExpectedException thrown = ExpectedException.none(); @Test public void testResponseTransformer() { @@ -58,27 +57,30 @@ public void testResponseTransformer() { OperationSnapshot operationSnapshot = GrpcOperationSnapshot.create( Operation.newBuilder().setResponse(Any.pack(inputMoney)).build()); - Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney); + assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney); } @Test public void testResponseTransformer_exception() { - thrown.expect(UnavailableException.class); ResponseTransformer transformer = ResponseTransformer.create(Money.class); Money inputMoney = Money.newBuilder().setCurrencyCode("USD").build(); Status status = Status.newBuilder().setCode(Code.UNAVAILABLE.value()).build(); OperationSnapshot operationSnapshot = GrpcOperationSnapshot.create( Operation.newBuilder().setResponse(Any.pack(inputMoney)).setError(status).build()); - Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney); + try { + transformer.apply(operationSnapshot); + Assert.fail("ResponseTransformer should have thrown an exception"); + } catch (UnavailableException expected) { + assertThat(expected) + .hasMessageThat() + .contains("failed with status = GrpcStatusCode{transportCode=UNAVAILABLE}"); + } } @Test public void testResponseTransformer_mismatchedTypes() { - thrown.expect(ApiException.class); - thrown.expectMessage("Failed to unpack object"); ResponseTransformer transformer = ResponseTransformer.create(Money.class); - Money inputMoney = Money.newBuilder().setCurrencyCode("USD").build(); Status status = Status.newBuilder().setCode(Code.OK.value()).build(); OperationSnapshot operationSnapshot = GrpcOperationSnapshot.create( @@ -86,7 +88,12 @@ public void testResponseTransformer_mismatchedTypes() { .setResponse(Any.pack(Color.getDefaultInstance())) .setError(status) .build()); - Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney); + try { + transformer.apply(operationSnapshot); + Assert.fail("ResponseTransformer should have thrown an exception"); + } catch (ApiException expected) { + assertThat(expected).hasMessageThat().contains("Failed to unpack object"); + } } @Test @@ -96,15 +103,12 @@ public void testMetadataTransformer() { OperationSnapshot operationSnapshot = GrpcOperationSnapshot.create( Operation.newBuilder().setMetadata(Any.pack(inputMoney)).build()); - Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney); + assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney); } @Test public void testMetadataTransformer_mismatchedTypes() { - thrown.expect(ApiException.class); - thrown.expectMessage("Failed to unpack object"); MetadataTransformer transformer = MetadataTransformer.create(Money.class); - Money inputMoney = Money.newBuilder().setCurrencyCode("USD").build(); Status status = Status.newBuilder().setCode(Code.OK.value()).build(); OperationSnapshot operationSnapshot = GrpcOperationSnapshot.create( @@ -112,6 +116,11 @@ public void testMetadataTransformer_mismatchedTypes() { .setMetadata(Any.pack(Color.getDefaultInstance())) .setError(status) .build()); - Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney); + try { + transformer.apply(operationSnapshot); + Assert.fail("MetadataTransformer should have thrown an exception"); + } catch (ApiException expected) { + assertThat(expected).hasMessageThat().contains("Failed to unpack object"); + } } } diff --git a/gax-grpc/src/test/java/com/google/api/gax/grpc/SettingsTest.java b/gax-grpc/src/test/java/com/google/api/gax/grpc/SettingsTest.java index eb3bb1169..812c1c1c3 100644 --- a/gax-grpc/src/test/java/com/google/api/gax/grpc/SettingsTest.java +++ b/gax-grpc/src/test/java/com/google/api/gax/grpc/SettingsTest.java @@ -57,9 +57,7 @@ import com.google.common.truth.Truth; import java.io.IOException; import org.apache.commons.lang3.builder.EqualsBuilder; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.mockito.Mockito; @@ -68,8 +66,6 @@ @RunWith(JUnit4.class) public class SettingsTest { - @Rule public ExpectedException thrown = ExpectedException.none(); - interface FakePagedListResponse extends PagedListResponse {} private static class FakeStubSettings extends StubSettings { diff --git a/gax-httpjson/src/test/java/com/google/api/gax/httpjson/ApiMessageOperationTransformersTest.java b/gax-httpjson/src/test/java/com/google/api/gax/httpjson/ApiMessageOperationTransformersTest.java index 783408632..4fca4629c 100644 --- a/gax-httpjson/src/test/java/com/google/api/gax/httpjson/ApiMessageOperationTransformersTest.java +++ b/gax-httpjson/src/test/java/com/google/api/gax/httpjson/ApiMessageOperationTransformersTest.java @@ -29,6 +29,8 @@ */ package com.google.api.gax.httpjson; +import static com.google.common.truth.Truth.assertThat; + import com.google.api.gax.httpjson.ApiMessageOperationTransformers.MetadataTransformer; import com.google.api.gax.httpjson.ApiMessageOperationTransformers.ResponseTransformer; import com.google.api.gax.httpjson.testing.FakeApiMessage; @@ -38,18 +40,15 @@ import com.google.api.gax.rpc.StatusCode.Code; import com.google.api.gax.rpc.UnavailableException; import com.google.common.collect.ImmutableMap; -import com.google.common.truth.Truth; import java.util.List; -import org.junit.Rule; +import org.junit.Assert; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; /** Tests for ApiMessageOperationTransformers. */ @RunWith(JUnit4.class) public class ApiMessageOperationTransformersTest { - @Rule public ExpectedException thrown = ExpectedException.none(); @Test public void testResponseTransformer() { @@ -61,35 +60,40 @@ public void testResponseTransformer() { new OperationSnapshotImpl( new FakeOperationMessage<>("Pending; no response method", emptyResponse, metadata)); - Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(emptyResponse); + assertThat(transformer.apply(operationSnapshot)).isEqualTo(emptyResponse); } @Test public void testResponseTransformer_exception() { - thrown.expect(UnavailableException.class); ResponseTransformer transformer = ResponseTransformer.create(EmptyMessage.class); EmptyMessage emptyResponse = EmptyMessage.getDefaultInstance(); FakeMetadataMessage metadata = new FakeMetadataMessage(Status.PENDING, Code.UNAVAILABLE); OperationSnapshot operationSnapshot = new OperationSnapshotImpl( new FakeOperationMessage<>("Unavailable; no response method", emptyResponse, metadata)); - - Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(emptyResponse); + try { + transformer.apply(operationSnapshot); + Assert.fail("ResponseTransformer should have thrown an exception"); + } catch (UnavailableException expected) { + assertThat(expected).hasMessageThat().contains("Unavailable; no response method"); + } } @Test public void testResponseTransformer_mismatchedTypes() { - thrown.expect(ApiException.class); - thrown.expectMessage("cannot be cast"); ResponseTransformer transformer = ResponseTransformer.create(EmptyMessage.class); FakeMetadataMessage metadata = new FakeMetadataMessage(Status.PENDING, Code.OK); ApiMessage bananaResponse = new FakeApiMessage(ImmutableMap.of("name", "banana"), null, null); - EmptyMessage emptyResponse = EmptyMessage.getDefaultInstance(); OperationSnapshot operationSnapshot = new OperationSnapshotImpl( new FakeOperationMessage<>("No response method", bananaResponse, metadata)); - Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(emptyResponse); + try { + transformer.apply(operationSnapshot); + Assert.fail("ResponseTransformer should have thrown an exception"); + } catch (ApiException expected) { + assertThat(expected).hasMessageThat().contains("cannot be cast"); + } } @Test @@ -100,13 +104,11 @@ public void testMetadataTransformer() { FakeMetadataMessage metadataMessage = new FakeMetadataMessage(Status.PENDING, Code.OK); FakeOperationMessage operation = new FakeOperationMessage<>("foo", returnType, metadataMessage); OperationSnapshot operationSnapshot = new OperationSnapshotImpl(operation); - Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(metadataMessage); + assertThat(transformer.apply(operationSnapshot)).isEqualTo(metadataMessage); } @Test public void testMetadataTransformer_mismatchedTypes() { - thrown.expect(ApiException.class); - thrown.expectMessage("cannot be cast"); MetadataTransformer transformer = MetadataTransformer.create(FakeOperationMessage.class); FakeMetadataMessage metadataMessage = new FakeMetadataMessage(Status.PENDING, Code.OK); @@ -115,7 +117,12 @@ public void testMetadataTransformer_mismatchedTypes() { FakeOperationMessage metadata = new FakeOperationMessage<>("No response method", bananaResponse, metadataMessage); OperationSnapshot operationSnapshot = new OperationSnapshotImpl(metadata); - Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(bananaResponse); + try { + transformer.apply(operationSnapshot); + Assert.fail("MetadataTransformer should have thrown an exception"); + } catch (ApiException expected) { + assertThat(expected).hasMessageThat().contains("cannot be cast"); + } } private enum Status { diff --git a/gax-httpjson/src/test/java/com/google/api/gax/httpjson/HttpJsonCallContextTest.java b/gax-httpjson/src/test/java/com/google/api/gax/httpjson/HttpJsonCallContextTest.java index 7860d5887..e3412d2b6 100644 --- a/gax-httpjson/src/test/java/com/google/api/gax/httpjson/HttpJsonCallContextTest.java +++ b/gax-httpjson/src/test/java/com/google/api/gax/httpjson/HttpJsonCallContextTest.java @@ -35,9 +35,8 @@ import com.google.api.gax.tracing.ApiTracer; import com.google.auth.Credentials; import com.google.common.truth.Truth; -import org.junit.Rule; +import org.junit.Assert; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.mockito.Mockito; @@ -45,12 +44,17 @@ @RunWith(JUnit4.class) public class HttpJsonCallContextTest { - @Rule public ExpectedException thrown = ExpectedException.none(); @Test public void testNullToSelfWrongType() { - thrown.expect(IllegalArgumentException.class); - HttpJsonCallContext.createDefault().nullToSelf(FakeCallContext.createDefault()); + try { + HttpJsonCallContext.createDefault().nullToSelf(FakeCallContext.createDefault()); + Assert.fail("HttpJsonCallContext should have thrown an exception"); + } catch (IllegalArgumentException expected) { + Truth.assertThat(expected) + .hasMessageThat() + .contains("context must be an instance of HttpJsonCallContext"); + } } @Test @@ -75,15 +79,26 @@ public void testWithTransportChannel() { @Test public void testWithTransportChannelWrongType() { - thrown.expect(IllegalArgumentException.class); - FakeChannel channel = new FakeChannel(); - HttpJsonCallContext.createDefault().withTransportChannel(FakeTransportChannel.create(channel)); + try { + FakeChannel channel = new FakeChannel(); + HttpJsonCallContext.createDefault() + .withTransportChannel(FakeTransportChannel.create(channel)); + Assert.fail("HttpJsonCallContext should have thrown an exception"); + } catch (IllegalArgumentException expected) { + Truth.assertThat(expected).hasMessageThat().contains("Expected HttpJsonTransportChannel"); + } } @Test public void testMergeWrongType() { - thrown.expect(IllegalArgumentException.class); - HttpJsonCallContext.createDefault().merge(FakeCallContext.createDefault()); + try { + HttpJsonCallContext.createDefault().merge(FakeCallContext.createDefault()); + Assert.fail("HttpJsonCallContext should have thrown an exception"); + } catch (IllegalArgumentException expected) { + Truth.assertThat(expected) + .hasMessageThat() + .contains("context must be an instance of HttpJsonCallContext"); + } } @Test diff --git a/gax-httpjson/src/test/java/com/google/api/gax/httpjson/RetryingTest.java b/gax-httpjson/src/test/java/com/google/api/gax/httpjson/RetryingTest.java index 5395d2306..dee85b241 100644 --- a/gax-httpjson/src/test/java/com/google/api/gax/httpjson/RetryingTest.java +++ b/gax-httpjson/src/test/java/com/google/api/gax/httpjson/RetryingTest.java @@ -29,6 +29,8 @@ */ package com.google.api.gax.httpjson; +import static com.google.common.truth.Truth.assertThat; + import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpResponseException; import com.google.api.client.http.HttpStatusCodes; @@ -48,15 +50,13 @@ import com.google.api.gax.rpc.UnaryCallable; import com.google.api.gax.rpc.UnknownException; import com.google.common.collect.ImmutableSet; -import com.google.common.truth.Truth; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.UncheckedExecutionException; import java.util.Set; import org.junit.After; +import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.mockito.Mockito; @@ -113,8 +113,6 @@ public void teardown() { executor.shutdownNow(); } - @Rule public ExpectedException thrown = ExpectedException.none(); - static ApiFuture immediateFailedFuture(Throwable t) { return ApiFutures.immediateFailedFuture(t); } @@ -132,7 +130,7 @@ public void retry() { createSettings(retryable, FAST_RETRY_SETTINGS); UnaryCallable callable = HttpJsonCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); - Truth.assertThat(callable.call(1)).isEqualTo(2); + assertThat(callable.call(1)).isEqualTo(2); } @Test(expected = ApiException.class) @@ -194,7 +192,7 @@ public void retryWithinMaxAttempts() { UnaryCallable callable = HttpJsonCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); callable.call(1); - Truth.assertThat(callable.call(1)).isEqualTo(2); + assertThat(callable.call(1)).isEqualTo(2); } @Test @@ -214,13 +212,11 @@ public void retryOnStatusUnknown() { createSettings(retryable, FAST_RETRY_SETTINGS); UnaryCallable callable = HttpJsonCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); - Truth.assertThat(callable.call(1)).isEqualTo(2); + assertThat(callable.call(1)).isEqualTo(2); } @Test public void retryOnUnexpectedException() { - thrown.expect(ApiException.class); - thrown.expectMessage("foobar"); ImmutableSet retryable = ImmutableSet.of(Code.UNKNOWN); Throwable throwable = new RuntimeException("foobar"); Mockito.when(callInt.futureCall((Integer) Mockito.any(), (ApiCallContext) Mockito.any())) @@ -229,13 +225,16 @@ public void retryOnUnexpectedException() { createSettings(retryable, FAST_RETRY_SETTINGS); UnaryCallable callable = HttpJsonCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); - callable.call(1); + try { + callable.call(1); + Assert.fail("Callable should have thrown an exception"); + } catch (ApiException expected) { + assertThat(expected).hasCauseThat().isSameInstanceAs(throwable); + } } @Test public void retryNoRecover() { - thrown.expect(ApiException.class); - thrown.expectMessage("foobar"); ImmutableSet retryable = ImmutableSet.of(Code.UNAVAILABLE); HttpResponseException httpResponseException = new HttpResponseException.Builder(STATUS_FAILED_PRECONDITION, "foobar", new HttpHeaders()) @@ -253,29 +252,37 @@ public void retryNoRecover() { createSettings(retryable, FAST_RETRY_SETTINGS); UnaryCallable callable = HttpJsonCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); - callable.call(1); + try { + callable.call(1); + Assert.fail("Callable should have thrown an exception"); + } catch (ApiException expected) { + assertThat(expected).isSameInstanceAs(apiException); + } } @Test public void retryKeepFailing() { - thrown.expect(UncheckedExecutionException.class); - thrown.expectMessage("foobar"); ImmutableSet retryable = ImmutableSet.of(Code.UNAVAILABLE); + HttpResponseException throwable = + new HttpResponseException.Builder( + HttpStatusCodes.STATUS_CODE_SERVICE_UNAVAILABLE, "Unavailable", new HttpHeaders()) + .build(); Mockito.when(callInt.futureCall((Integer) Mockito.any(), (ApiCallContext) Mockito.any())) - .thenReturn( - RetryingTest.immediateFailedFuture( - new HttpResponseException.Builder( - HttpStatusCodes.STATUS_CODE_SERVICE_UNAVAILABLE, - "foobar", - new HttpHeaders()) - .build())); + .thenReturn(RetryingTest.immediateFailedFuture(throwable)); UnaryCallSettings callSettings = createSettings(retryable, FAST_RETRY_SETTINGS); UnaryCallable callable = HttpJsonCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); // Need to advance time inside the call. ApiFuture future = callable.futureCall(1); - Futures.getUnchecked(future); + + try { + Futures.getUnchecked(future); + Assert.fail("Callable should have thrown an exception"); + } catch (UncheckedExecutionException expected) { + assertThat(expected).hasCauseThat().isInstanceOf(ApiException.class); + assertThat(expected).hasCauseThat().hasMessageThat().contains("Unavailable"); + } } @Test @@ -311,10 +318,11 @@ public void testKnownStatusCode() { HttpJsonCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); try { callable.call(1); - } catch (FailedPreconditionException exception) { - Truth.assertThat(((HttpJsonStatusCode) exception.getStatusCode()).getTransportCode()) + Assert.fail("Callable should have thrown an exception"); + } catch (FailedPreconditionException expected) { + assertThat(((HttpJsonStatusCode) expected.getStatusCode()).getTransportCode()) .isEqualTo(STATUS_FAILED_PRECONDITION); - Truth.assertThat(exception.getMessage()).contains(HttpJsonStatusCode.FAILED_PRECONDITION); + assertThat(expected.getMessage()).contains(HttpJsonStatusCode.FAILED_PRECONDITION); } } @@ -331,8 +339,9 @@ public void testUnknownStatusCode() { HttpJsonCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); try { callable.call(1); - } catch (UnknownException exception) { - Truth.assertThat(exception.getMessage()).isEqualTo("java.lang.RuntimeException: unknown"); + Assert.fail("Callable should have thrown an exception"); + } catch (UnknownException expected) { + assertThat(expected.getMessage()).isEqualTo("java.lang.RuntimeException: unknown"); } } diff --git a/gax/src/test/java/com/google/api/gax/batching/ThresholdBatcherTest.java b/gax/src/test/java/com/google/api/gax/batching/ThresholdBatcherTest.java index 0d6557dfe..45eaa84b7 100644 --- a/gax/src/test/java/com/google/api/gax/batching/ThresholdBatcherTest.java +++ b/gax/src/test/java/com/google/api/gax/batching/ThresholdBatcherTest.java @@ -41,9 +41,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import org.junit.Assert; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.threeten.bp.Duration; public class ThresholdBatcherTest { @@ -105,8 +103,6 @@ public long count(SimpleBatch t) { }); } - @Rule public ExpectedException thrown = ExpectedException.none(); - private static class SimpleBatch { private final List integers = new ArrayList<>(); @@ -220,15 +216,19 @@ public void testBatchingWithDelay() throws Exception { @Test public void testExceptionWithNullFlowController() { - thrown.expect(NullPointerException.class); - ThresholdBatcher.newBuilder() - .setThresholds(BatchingThresholds.create(100)) - .setExecutor(EXECUTOR) - .setMaxDelay(Duration.ofMillis(10000)) - .setReceiver( - new AccumulatingBatchReceiver(ApiFutures.immediateFuture(null))) - .setBatchMerger(new SimpleBatchMerger()) - .build(); + try { + ThresholdBatcher.newBuilder() + .setThresholds(BatchingThresholds.create(100)) + .setExecutor(EXECUTOR) + .setMaxDelay(Duration.ofMillis(10000)) + .setReceiver( + new AccumulatingBatchReceiver(ApiFutures.immediateFuture(null))) + .setBatchMerger(new SimpleBatchMerger()) + .build(); + Assert.fail("ThresholdBatcher should have thrown an exception"); + } catch (NullPointerException expected) { + assertThat(expected).isInstanceOf(NullPointerException.class); + } } @Test diff --git a/gax/src/test/java/com/google/api/gax/rpc/ApiExceptionsTest.java b/gax/src/test/java/com/google/api/gax/rpc/ApiExceptionsTest.java index 9043983b7..411f21b2b 100644 --- a/gax/src/test/java/com/google/api/gax/rpc/ApiExceptionsTest.java +++ b/gax/src/test/java/com/google/api/gax/rpc/ApiExceptionsTest.java @@ -40,15 +40,13 @@ import com.google.common.util.concurrent.UncheckedExecutionException; import java.io.IOException; import java.util.concurrent.Executors; -import org.junit.Rule; +import org.junit.Assert; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) public class ApiExceptionsTest { - @Rule public ExpectedException thrown = ExpectedException.none(); @Test public void noException() { @@ -58,23 +56,36 @@ public void noException() { @Test public void throwsApiException() { - thrown.expect(ApiException.class); - ApiExceptions.callAndTranslateApiException( - ApiFutures.immediateFailedFuture( - new UnavailableException(null, FakeStatusCode.of(StatusCode.Code.UNAVAILABLE), false))); + Exception throwable = + new UnavailableException(null, FakeStatusCode.of(StatusCode.Code.UNAVAILABLE), false); + try { + ApiExceptions.callAndTranslateApiException(ApiFutures.immediateFailedFuture(throwable)); + Assert.fail("ApiExceptions should have thrown an exception"); + } catch (ApiException expected) { + assertThat(expected).isSameInstanceAs(throwable); + } } @Test public void throwsIOException() { - thrown.expect(UncheckedExecutionException.class); - ApiExceptions.callAndTranslateApiException(ApiFutures.immediateFailedFuture(new IOException())); + try { + ApiExceptions.callAndTranslateApiException( + ApiFutures.immediateFailedFuture(new IOException())); + Assert.fail("ApiExceptions should have thrown an exception"); + } catch (UncheckedExecutionException expected) { + assertThat(expected).hasCauseThat().isInstanceOf(IOException.class); + } } @Test public void throwsRuntimeException() { - thrown.expect(IllegalArgumentException.class); - ApiExceptions.callAndTranslateApiException( - ApiFutures.immediateFailedFuture(new IllegalArgumentException())); + try { + ApiExceptions.callAndTranslateApiException( + ApiFutures.immediateFailedFuture(new IllegalArgumentException())); + Assert.fail("ApiExceptions should have thrown an exception"); + } catch (IllegalArgumentException expected) { + assertThat(expected).isInstanceOf(IllegalArgumentException.class); + } } /** diff --git a/gax/src/test/java/com/google/api/gax/rpc/BatchedRequestIssuerTest.java b/gax/src/test/java/com/google/api/gax/rpc/BatchedRequestIssuerTest.java index c53e64ef7..ebc933d98 100644 --- a/gax/src/test/java/com/google/api/gax/rpc/BatchedRequestIssuerTest.java +++ b/gax/src/test/java/com/google/api/gax/rpc/BatchedRequestIssuerTest.java @@ -29,28 +29,25 @@ */ package com.google.api.gax.rpc; -import com.google.common.truth.Truth; +import static com.google.common.truth.Truth.assertThat; + import java.util.concurrent.ExecutionException; import org.junit.Assert; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; public class BatchedRequestIssuerTest { - @Rule public ExpectedException thrown = ExpectedException.none(); - @Test public void test() throws Exception { BatchedFuture batchedFuture = BatchedFuture.create(); BatchedRequestIssuer issuer = new BatchedRequestIssuer<>(batchedFuture, 2); issuer.setResponse(1); - Truth.assertThat(batchedFuture.isDone()).isFalse(); + assertThat(batchedFuture.isDone()).isFalse(); issuer.sendResult(); - Truth.assertThat(batchedFuture.isDone()).isTrue(); - Truth.assertThat(batchedFuture.get()).isEqualTo(1); + assertThat(batchedFuture.isDone()).isTrue(); + assertThat(batchedFuture.get()).isEqualTo(1); } @Test @@ -59,11 +56,11 @@ public void testNullResult() throws Exception { BatchedRequestIssuer issuer = new BatchedRequestIssuer<>(batchedFuture, 2); issuer.setResponse(null); - Truth.assertThat(batchedFuture.isDone()).isFalse(); + assertThat(batchedFuture.isDone()).isFalse(); issuer.sendResult(); - Truth.assertThat(batchedFuture.isDone()).isTrue(); - Truth.assertThat(batchedFuture.get()).isNull(); + assertThat(batchedFuture.isDone()).isTrue(); + assertThat(batchedFuture.get()).isNull(); } @Test @@ -74,46 +71,57 @@ public void testException() throws Exception { BatchedRequestIssuer issuer = new BatchedRequestIssuer<>(batchedFuture, 2); issuer.setException(thrownException); - Truth.assertThat(batchedFuture.isDone()).isFalse(); + assertThat(batchedFuture.isDone()).isFalse(); issuer.sendResult(); - Truth.assertThat(batchedFuture.isDone()).isTrue(); + assertThat(batchedFuture.isDone()).isTrue(); try { batchedFuture.get(); Assert.fail("BatchedFuture should have thrown an exception"); } catch (ExecutionException e) { - Truth.assertThat(e.getCause()).isInstanceOf(IllegalArgumentException.class); + assertThat(e.getCause()).isInstanceOf(IllegalArgumentException.class); } } @Test public void testNoResult() { - thrown.expect(IllegalStateException.class); - - BatchedFuture batchedFuture = BatchedFuture.create(); - BatchedRequestIssuer issuer = new BatchedRequestIssuer<>(batchedFuture, 2); - issuer.sendResult(); + try { + BatchedFuture batchedFuture = BatchedFuture.create(); + BatchedRequestIssuer issuer = new BatchedRequestIssuer<>(batchedFuture, 2); + issuer.sendResult(); + Assert.fail("BatchedFuture should have thrown an exception"); + } catch (IllegalStateException expected) { + assertThat(expected) + .hasMessageThat() + .contains("Neither response nor exception were set in BatchedRequestIssuer"); + } } @Test public void testResponseAndException() { - thrown.expect(IllegalStateException.class); - - Exception thrownException = new IllegalArgumentException("bad!"); - BatchedFuture batchedFuture = BatchedFuture.create(); - BatchedRequestIssuer issuer = new BatchedRequestIssuer<>(batchedFuture, 2); - issuer.setResponse(1); - issuer.setException(thrownException); + try { + Exception thrownException = new IllegalArgumentException("bad!"); + BatchedFuture batchedFuture = BatchedFuture.create(); + BatchedRequestIssuer issuer = new BatchedRequestIssuer<>(batchedFuture, 2); + issuer.setResponse(1); + issuer.setException(thrownException); + Assert.fail("BatchedFuture should have thrown an exception"); + } catch (IllegalStateException expected) { + assertThat(expected).hasMessageThat().contains("Cannot set both exception and response"); + } } @Test public void testExceptionAndResponse() { - thrown.expect(IllegalStateException.class); - - Exception thrownException = new IllegalArgumentException("bad!"); - BatchedFuture batchedFuture = BatchedFuture.create(); - BatchedRequestIssuer issuer = new BatchedRequestIssuer<>(batchedFuture, 2); - issuer.setException(thrownException); - issuer.setResponse(1); + try { + Exception thrownException = new IllegalArgumentException("bad!"); + BatchedFuture batchedFuture = BatchedFuture.create(); + BatchedRequestIssuer issuer = new BatchedRequestIssuer<>(batchedFuture, 2); + issuer.setException(thrownException); + issuer.setResponse(1); + Assert.fail("BatchedFuture should have thrown an exception"); + } catch (IllegalStateException expected) { + assertThat(expected).hasMessageThat().contains("Cannot set both exception and response"); + } } } diff --git a/gax/src/test/java/com/google/api/gax/rpc/CancellationTest.java b/gax/src/test/java/com/google/api/gax/rpc/CancellationTest.java index 55c974109..0d538577a 100644 --- a/gax/src/test/java/com/google/api/gax/rpc/CancellationTest.java +++ b/gax/src/test/java/com/google/api/gax/rpc/CancellationTest.java @@ -29,6 +29,8 @@ */ package com.google.api.gax.rpc; +import static com.google.common.truth.Truth.assertThat; + import com.google.api.core.AbstractApiFuture; import com.google.api.core.ApiFuture; import com.google.api.core.SettableApiFuture; @@ -47,10 +49,9 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ScheduledThreadPoolExecutor; import org.junit.After; +import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.mockito.Mockito; @@ -88,8 +89,6 @@ public class CancellationTest { private RecordingScheduler executor; private ClientContext clientContext; - @Rule public ExpectedException thrown = ExpectedException.none(); - @Before public void resetClock() { fakeClock = new FakeApiClock(System.nanoTime()); @@ -110,18 +109,22 @@ public void teardown() { @Test public void cancellationBeforeGetOnRetryingCallable() throws Exception { - thrown.expect(CancellationException.class); - Mockito.when(callInt.futureCall((Integer) Mockito.any(), (ApiCallContext) Mockito.any())) - .thenReturn(SettableApiFuture.create()); + try { + Mockito.when(callInt.futureCall((Integer) Mockito.any(), (ApiCallContext) Mockito.any())) + .thenReturn(SettableApiFuture.create()); - UnaryCallSettings callSettings = - RetryingTest.createSettings(FAST_RETRY_SETTINGS); - UnaryCallable callable = - FakeCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); + UnaryCallSettings callSettings = + RetryingTest.createSettings(FAST_RETRY_SETTINGS); + UnaryCallable callable = + FakeCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); - ApiFuture resultFuture = callable.futureCall(0); - resultFuture.cancel(true); - resultFuture.get(); + ApiFuture resultFuture = callable.futureCall(0); + resultFuture.cancel(true); + resultFuture.get(); + Assert.fail("Callable should have thrown an exception"); + } catch (CancellationException expected) { + assertThat(expected).hasMessageThat().contains("Task was cancelled"); + } } private static class CancellationTrackingFuture extends AbstractApiFuture { diff --git a/gax/src/test/java/com/google/api/gax/rpc/FirstElementCallableTest.java b/gax/src/test/java/com/google/api/gax/rpc/FirstElementCallableTest.java index 100e90f01..e26ce719e 100644 --- a/gax/src/test/java/com/google/api/gax/rpc/FirstElementCallableTest.java +++ b/gax/src/test/java/com/google/api/gax/rpc/FirstElementCallableTest.java @@ -37,9 +37,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -48,8 +46,6 @@ public class FirstElementCallableTest { private MockServerStreamingCallable upstream; private FirstElementCallable callable; - @Rule public ExpectedException expectedException = ExpectedException.none(); - @Before public void setup() { upstream = new MockServerStreamingCallable<>(); diff --git a/gax/src/test/java/com/google/api/gax/rpc/RetryingTest.java b/gax/src/test/java/com/google/api/gax/rpc/RetryingTest.java index 585b35bd0..c39872ad5 100644 --- a/gax/src/test/java/com/google/api/gax/rpc/RetryingTest.java +++ b/gax/src/test/java/com/google/api/gax/rpc/RetryingTest.java @@ -29,6 +29,8 @@ */ package com.google.api.gax.rpc; +import static com.google.common.truth.Truth.assertThat; + import com.google.api.core.ApiFuture; import com.google.api.core.ApiFutures; import com.google.api.gax.core.FakeApiClock; @@ -41,14 +43,12 @@ import com.google.api.gax.rpc.testing.FakeStatusCode; import com.google.api.gax.rpc.testing.FakeTransportChannel; import com.google.common.collect.ImmutableSet; -import com.google.common.truth.Truth; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.UncheckedExecutionException; import org.junit.After; +import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.mockito.Mockito; @@ -93,8 +93,6 @@ public void teardown() { executor.shutdownNow(); } - @Rule public ExpectedException thrown = ExpectedException.none(); - static ApiFuture immediateFailedFuture(Throwable t) { return ApiFutures.immediateFailedFuture(t); } @@ -196,43 +194,52 @@ public void retryOnStatusUnknown() { @Test public void retryOnUnexpectedException() { - thrown.expect(ApiException.class); - thrown.expectMessage("foobar"); Throwable throwable = new UnknownException("foobar", null, FakeStatusCode.of(StatusCode.Code.UNKNOWN), false); Mockito.when(callInt.futureCall((Integer) Mockito.any(), (ApiCallContext) Mockito.any())) .thenReturn(RetryingTest.immediateFailedFuture(throwable)); - assertRetrying(FAST_RETRY_SETTINGS); + try { + assertRetrying(FAST_RETRY_SETTINGS); + Assert.fail("Callable should have thrown an exception"); + } catch (ApiException expected) { + assertThat(expected).isSameInstanceAs(throwable); + } } @Test public void retryNoRecover() { - thrown.expect(ApiException.class); - thrown.expectMessage("foobar"); + Throwable throwable = + new FailedPreconditionException( + "foobar", null, FakeStatusCode.of(StatusCode.Code.FAILED_PRECONDITION), false); Mockito.when(callInt.futureCall((Integer) Mockito.any(), (ApiCallContext) Mockito.any())) - .thenReturn( - RetryingTest.immediateFailedFuture( - new FailedPreconditionException( - "foobar", null, FakeStatusCode.of(StatusCode.Code.FAILED_PRECONDITION), false))) + .thenReturn(RetryingTest.immediateFailedFuture(throwable)) .thenReturn(ApiFutures.immediateFuture(2)); - assertRetrying(FAST_RETRY_SETTINGS); + try { + assertRetrying(FAST_RETRY_SETTINGS); + Assert.fail("Callable should have thrown an exception"); + } catch (ApiException expected) { + assertThat(expected).isSameInstanceAs(throwable); + } } @Test public void retryKeepFailing() { - thrown.expect(UncheckedExecutionException.class); - thrown.expectMessage("foobar"); + Throwable throwable = + new UnavailableException( + "foobar", null, FakeStatusCode.of(StatusCode.Code.UNAVAILABLE), true); Mockito.when(callInt.futureCall((Integer) Mockito.any(), (ApiCallContext) Mockito.any())) - .thenReturn( - RetryingTest.immediateFailedFuture( - new UnavailableException( - "foobar", null, FakeStatusCode.of(StatusCode.Code.UNAVAILABLE), true))); + .thenReturn(RetryingTest.immediateFailedFuture(throwable)); UnaryCallSettings callSettings = createSettings(FAST_RETRY_SETTINGS); UnaryCallable callable = FakeCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); // Need to advance time inside the call. ApiFuture future = callable.futureCall(1); - Futures.getUnchecked(future); + try { + Futures.getUnchecked(future); + Assert.fail("Callable should have thrown an exception"); + } catch (UncheckedExecutionException expected) { + assertThat(expected).hasCauseThat().isSameInstanceAs(throwable); + } } @Test @@ -251,14 +258,14 @@ public void testKnownStatusCode() { FakeCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); try { callable.call(1); - } catch (FailedPreconditionException exception) { - Truth.assertThat(exception.getMessage()).isEqualTo("known"); + Assert.fail("Callable should have thrown an exception"); + } catch (FailedPreconditionException expected) { + assertThat(expected.getMessage()).isEqualTo("known"); } } @Test public void testUnknownStatusCode() { - thrown.expect(RuntimeException.class); ImmutableSet retryable = ImmutableSet.of(); Mockito.when(callInt.futureCall((Integer) Mockito.any(), (ApiCallContext) Mockito.any())) .thenReturn(RetryingTest.immediateFailedFuture(new RuntimeException("unknown"))); @@ -268,7 +275,12 @@ public void testUnknownStatusCode() { .build(); UnaryCallable callable = FakeCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); - callable.call(1); + try { + callable.call(1); + Assert.fail("Callable should have thrown an exception"); + } catch (RuntimeException expected) { + assertThat(expected).isInstanceOf(RuntimeException.class); + } } public static UnaryCallSettings createSettings(RetrySettings retrySettings) { @@ -282,6 +294,6 @@ private void assertRetrying(RetrySettings retrySettings) { UnaryCallSettings callSettings = createSettings(retrySettings); UnaryCallable callable = FakeCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); - Truth.assertThat(callable.call(1)).isEqualTo(2); + assertThat(callable.call(1)).isEqualTo(2); } } diff --git a/gax/src/test/java/com/google/api/gax/rpc/SpoolingCallableTest.java b/gax/src/test/java/com/google/api/gax/rpc/SpoolingCallableTest.java index 4ac073c72..cad95dee5 100644 --- a/gax/src/test/java/com/google/api/gax/rpc/SpoolingCallableTest.java +++ b/gax/src/test/java/com/google/api/gax/rpc/SpoolingCallableTest.java @@ -38,10 +38,9 @@ import java.util.List; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; +import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -50,8 +49,6 @@ public class SpoolingCallableTest { private MockServerStreamingCallable upstream; private SpoolingCallable callable; - @Rule public ExpectedException expectedException = ExpectedException.none(); - @Before public void setup() { upstream = new MockServerStreamingCallable<>(); @@ -91,8 +88,12 @@ public void testEarlyTermination() throws Exception { .onError(new RuntimeException("Some other upstream cancellation indicator")); // However the inner cancellation exception will be masked by an outer CancellationException - expectedException.expect(CancellationException.class); - result.get(); + try { + result.get(); + Assert.fail("Callable should have thrown an exception"); + } catch (CancellationException expected) { + Truth.assertThat(expected).hasMessageThat().contains("Task was cancelled."); + } } @Test diff --git a/gax/src/test/java/com/google/api/gax/rpc/UnaryCallableTest.java b/gax/src/test/java/com/google/api/gax/rpc/UnaryCallableTest.java index 47e4286a5..ffc5185a7 100644 --- a/gax/src/test/java/com/google/api/gax/rpc/UnaryCallableTest.java +++ b/gax/src/test/java/com/google/api/gax/rpc/UnaryCallableTest.java @@ -34,9 +34,7 @@ import com.google.api.gax.rpc.testing.FakeSimpleApi.StashCallable; import com.google.auth.Credentials; import com.google.common.truth.Truth; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.mockito.Mockito; @@ -44,7 +42,6 @@ /** Tests for {@link UnaryCallable}. */ @RunWith(JUnit4.class) public class UnaryCallableTest { - @Rule public ExpectedException thrown = ExpectedException.none(); @Test public void simpleCall() throws Exception { From a65b19f690350c0549259c7c59216e0cedeb0d3f Mon Sep 17 00:00:00 2001 From: Rahul Kesharwani Date: Thu, 9 Apr 2020 15:30:26 +0530 Subject: [PATCH 2/2] chore: removed Truth.assertThat as static import --- .../grpc/ProtoOperationTransformersTest.java | 13 ++++---- .../ApiMessageOperationTransformersTest.java | 13 ++++---- .../google/api/gax/httpjson/RetryingTest.java | 23 +++++++------- .../api/gax/rpc/BatchedRequestIssuerTest.java | 31 ++++++++++--------- .../google/api/gax/rpc/CancellationTest.java | 4 +-- .../com/google/api/gax/rpc/RetryingTest.java | 15 +++++---- .../api/gax/rpc/SpoolingCallableTest.java | 8 ++--- 7 files changed, 51 insertions(+), 56 deletions(-) diff --git a/gax-grpc/src/test/java/com/google/api/gax/grpc/ProtoOperationTransformersTest.java b/gax-grpc/src/test/java/com/google/api/gax/grpc/ProtoOperationTransformersTest.java index d05bf3b65..809e8d7a8 100644 --- a/gax-grpc/src/test/java/com/google/api/gax/grpc/ProtoOperationTransformersTest.java +++ b/gax-grpc/src/test/java/com/google/api/gax/grpc/ProtoOperationTransformersTest.java @@ -29,13 +29,12 @@ */ package com.google.api.gax.grpc; -import static com.google.common.truth.Truth.assertThat; - import com.google.api.gax.grpc.ProtoOperationTransformers.MetadataTransformer; import com.google.api.gax.grpc.ProtoOperationTransformers.ResponseTransformer; import com.google.api.gax.longrunning.OperationSnapshot; import com.google.api.gax.rpc.ApiException; import com.google.api.gax.rpc.UnavailableException; +import com.google.common.truth.Truth; import com.google.longrunning.Operation; import com.google.protobuf.Any; import com.google.rpc.Status; @@ -57,7 +56,7 @@ public void testResponseTransformer() { OperationSnapshot operationSnapshot = GrpcOperationSnapshot.create( Operation.newBuilder().setResponse(Any.pack(inputMoney)).build()); - assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney); + Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney); } @Test @@ -72,7 +71,7 @@ public void testResponseTransformer_exception() { transformer.apply(operationSnapshot); Assert.fail("ResponseTransformer should have thrown an exception"); } catch (UnavailableException expected) { - assertThat(expected) + Truth.assertThat(expected) .hasMessageThat() .contains("failed with status = GrpcStatusCode{transportCode=UNAVAILABLE}"); } @@ -92,7 +91,7 @@ public void testResponseTransformer_mismatchedTypes() { transformer.apply(operationSnapshot); Assert.fail("ResponseTransformer should have thrown an exception"); } catch (ApiException expected) { - assertThat(expected).hasMessageThat().contains("Failed to unpack object"); + Truth.assertThat(expected).hasMessageThat().contains("Failed to unpack object"); } } @@ -103,7 +102,7 @@ public void testMetadataTransformer() { OperationSnapshot operationSnapshot = GrpcOperationSnapshot.create( Operation.newBuilder().setMetadata(Any.pack(inputMoney)).build()); - assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney); + Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney); } @Test @@ -120,7 +119,7 @@ public void testMetadataTransformer_mismatchedTypes() { transformer.apply(operationSnapshot); Assert.fail("MetadataTransformer should have thrown an exception"); } catch (ApiException expected) { - assertThat(expected).hasMessageThat().contains("Failed to unpack object"); + Truth.assertThat(expected).hasMessageThat().contains("Failed to unpack object"); } } } diff --git a/gax-httpjson/src/test/java/com/google/api/gax/httpjson/ApiMessageOperationTransformersTest.java b/gax-httpjson/src/test/java/com/google/api/gax/httpjson/ApiMessageOperationTransformersTest.java index 4fca4629c..edf0f56ce 100644 --- a/gax-httpjson/src/test/java/com/google/api/gax/httpjson/ApiMessageOperationTransformersTest.java +++ b/gax-httpjson/src/test/java/com/google/api/gax/httpjson/ApiMessageOperationTransformersTest.java @@ -29,8 +29,6 @@ */ package com.google.api.gax.httpjson; -import static com.google.common.truth.Truth.assertThat; - import com.google.api.gax.httpjson.ApiMessageOperationTransformers.MetadataTransformer; import com.google.api.gax.httpjson.ApiMessageOperationTransformers.ResponseTransformer; import com.google.api.gax.httpjson.testing.FakeApiMessage; @@ -40,6 +38,7 @@ import com.google.api.gax.rpc.StatusCode.Code; import com.google.api.gax.rpc.UnavailableException; import com.google.common.collect.ImmutableMap; +import com.google.common.truth.Truth; import java.util.List; import org.junit.Assert; import org.junit.Test; @@ -60,7 +59,7 @@ public void testResponseTransformer() { new OperationSnapshotImpl( new FakeOperationMessage<>("Pending; no response method", emptyResponse, metadata)); - assertThat(transformer.apply(operationSnapshot)).isEqualTo(emptyResponse); + Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(emptyResponse); } @Test @@ -75,7 +74,7 @@ public void testResponseTransformer_exception() { transformer.apply(operationSnapshot); Assert.fail("ResponseTransformer should have thrown an exception"); } catch (UnavailableException expected) { - assertThat(expected).hasMessageThat().contains("Unavailable; no response method"); + Truth.assertThat(expected).hasMessageThat().contains("Unavailable; no response method"); } } @@ -92,7 +91,7 @@ public void testResponseTransformer_mismatchedTypes() { transformer.apply(operationSnapshot); Assert.fail("ResponseTransformer should have thrown an exception"); } catch (ApiException expected) { - assertThat(expected).hasMessageThat().contains("cannot be cast"); + Truth.assertThat(expected).hasMessageThat().contains("cannot be cast"); } } @@ -104,7 +103,7 @@ public void testMetadataTransformer() { FakeMetadataMessage metadataMessage = new FakeMetadataMessage(Status.PENDING, Code.OK); FakeOperationMessage operation = new FakeOperationMessage<>("foo", returnType, metadataMessage); OperationSnapshot operationSnapshot = new OperationSnapshotImpl(operation); - assertThat(transformer.apply(operationSnapshot)).isEqualTo(metadataMessage); + Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(metadataMessage); } @Test @@ -121,7 +120,7 @@ public void testMetadataTransformer_mismatchedTypes() { transformer.apply(operationSnapshot); Assert.fail("MetadataTransformer should have thrown an exception"); } catch (ApiException expected) { - assertThat(expected).hasMessageThat().contains("cannot be cast"); + Truth.assertThat(expected).hasMessageThat().contains("cannot be cast"); } } diff --git a/gax-httpjson/src/test/java/com/google/api/gax/httpjson/RetryingTest.java b/gax-httpjson/src/test/java/com/google/api/gax/httpjson/RetryingTest.java index dee85b241..c6a68b661 100644 --- a/gax-httpjson/src/test/java/com/google/api/gax/httpjson/RetryingTest.java +++ b/gax-httpjson/src/test/java/com/google/api/gax/httpjson/RetryingTest.java @@ -29,8 +29,6 @@ */ package com.google.api.gax.httpjson; -import static com.google.common.truth.Truth.assertThat; - import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpResponseException; import com.google.api.client.http.HttpStatusCodes; @@ -50,6 +48,7 @@ import com.google.api.gax.rpc.UnaryCallable; import com.google.api.gax.rpc.UnknownException; import com.google.common.collect.ImmutableSet; +import com.google.common.truth.Truth; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.UncheckedExecutionException; import java.util.Set; @@ -130,7 +129,7 @@ public void retry() { createSettings(retryable, FAST_RETRY_SETTINGS); UnaryCallable callable = HttpJsonCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); - assertThat(callable.call(1)).isEqualTo(2); + Truth.assertThat(callable.call(1)).isEqualTo(2); } @Test(expected = ApiException.class) @@ -192,7 +191,7 @@ public void retryWithinMaxAttempts() { UnaryCallable callable = HttpJsonCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); callable.call(1); - assertThat(callable.call(1)).isEqualTo(2); + Truth.assertThat(callable.call(1)).isEqualTo(2); } @Test @@ -212,7 +211,7 @@ public void retryOnStatusUnknown() { createSettings(retryable, FAST_RETRY_SETTINGS); UnaryCallable callable = HttpJsonCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); - assertThat(callable.call(1)).isEqualTo(2); + Truth.assertThat(callable.call(1)).isEqualTo(2); } @Test @@ -229,7 +228,7 @@ public void retryOnUnexpectedException() { callable.call(1); Assert.fail("Callable should have thrown an exception"); } catch (ApiException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(throwable); + Truth.assertThat(expected).hasCauseThat().isSameInstanceAs(throwable); } } @@ -256,7 +255,7 @@ public void retryNoRecover() { callable.call(1); Assert.fail("Callable should have thrown an exception"); } catch (ApiException expected) { - assertThat(expected).isSameInstanceAs(apiException); + Truth.assertThat(expected).isSameInstanceAs(apiException); } } @@ -280,8 +279,8 @@ public void retryKeepFailing() { Futures.getUnchecked(future); Assert.fail("Callable should have thrown an exception"); } catch (UncheckedExecutionException expected) { - assertThat(expected).hasCauseThat().isInstanceOf(ApiException.class); - assertThat(expected).hasCauseThat().hasMessageThat().contains("Unavailable"); + Truth.assertThat(expected).hasCauseThat().isInstanceOf(ApiException.class); + Truth.assertThat(expected).hasCauseThat().hasMessageThat().contains("Unavailable"); } } @@ -320,9 +319,9 @@ public void testKnownStatusCode() { callable.call(1); Assert.fail("Callable should have thrown an exception"); } catch (FailedPreconditionException expected) { - assertThat(((HttpJsonStatusCode) expected.getStatusCode()).getTransportCode()) + Truth.assertThat(((HttpJsonStatusCode) expected.getStatusCode()).getTransportCode()) .isEqualTo(STATUS_FAILED_PRECONDITION); - assertThat(expected.getMessage()).contains(HttpJsonStatusCode.FAILED_PRECONDITION); + Truth.assertThat(expected.getMessage()).contains(HttpJsonStatusCode.FAILED_PRECONDITION); } } @@ -341,7 +340,7 @@ public void testUnknownStatusCode() { callable.call(1); Assert.fail("Callable should have thrown an exception"); } catch (UnknownException expected) { - assertThat(expected.getMessage()).isEqualTo("java.lang.RuntimeException: unknown"); + Truth.assertThat(expected.getMessage()).isEqualTo("java.lang.RuntimeException: unknown"); } } diff --git a/gax/src/test/java/com/google/api/gax/rpc/BatchedRequestIssuerTest.java b/gax/src/test/java/com/google/api/gax/rpc/BatchedRequestIssuerTest.java index ebc933d98..293252afc 100644 --- a/gax/src/test/java/com/google/api/gax/rpc/BatchedRequestIssuerTest.java +++ b/gax/src/test/java/com/google/api/gax/rpc/BatchedRequestIssuerTest.java @@ -29,8 +29,7 @@ */ package com.google.api.gax.rpc; -import static com.google.common.truth.Truth.assertThat; - +import com.google.common.truth.Truth; import java.util.concurrent.ExecutionException; import org.junit.Assert; import org.junit.Test; @@ -43,11 +42,11 @@ public void test() throws Exception { BatchedRequestIssuer issuer = new BatchedRequestIssuer<>(batchedFuture, 2); issuer.setResponse(1); - assertThat(batchedFuture.isDone()).isFalse(); + Truth.assertThat(batchedFuture.isDone()).isFalse(); issuer.sendResult(); - assertThat(batchedFuture.isDone()).isTrue(); - assertThat(batchedFuture.get()).isEqualTo(1); + Truth.assertThat(batchedFuture.isDone()).isTrue(); + Truth.assertThat(batchedFuture.get()).isEqualTo(1); } @Test @@ -56,11 +55,11 @@ public void testNullResult() throws Exception { BatchedRequestIssuer issuer = new BatchedRequestIssuer<>(batchedFuture, 2); issuer.setResponse(null); - assertThat(batchedFuture.isDone()).isFalse(); + Truth.assertThat(batchedFuture.isDone()).isFalse(); issuer.sendResult(); - assertThat(batchedFuture.isDone()).isTrue(); - assertThat(batchedFuture.get()).isNull(); + Truth.assertThat(batchedFuture.isDone()).isTrue(); + Truth.assertThat(batchedFuture.get()).isNull(); } @Test @@ -71,15 +70,15 @@ public void testException() throws Exception { BatchedRequestIssuer issuer = new BatchedRequestIssuer<>(batchedFuture, 2); issuer.setException(thrownException); - assertThat(batchedFuture.isDone()).isFalse(); + Truth.assertThat(batchedFuture.isDone()).isFalse(); issuer.sendResult(); - assertThat(batchedFuture.isDone()).isTrue(); + Truth.assertThat(batchedFuture.isDone()).isTrue(); try { batchedFuture.get(); Assert.fail("BatchedFuture should have thrown an exception"); } catch (ExecutionException e) { - assertThat(e.getCause()).isInstanceOf(IllegalArgumentException.class); + Truth.assertThat(e.getCause()).isInstanceOf(IllegalArgumentException.class); } } @@ -91,7 +90,7 @@ public void testNoResult() { issuer.sendResult(); Assert.fail("BatchedFuture should have thrown an exception"); } catch (IllegalStateException expected) { - assertThat(expected) + Truth.assertThat(expected) .hasMessageThat() .contains("Neither response nor exception were set in BatchedRequestIssuer"); } @@ -107,7 +106,9 @@ public void testResponseAndException() { issuer.setException(thrownException); Assert.fail("BatchedFuture should have thrown an exception"); } catch (IllegalStateException expected) { - assertThat(expected).hasMessageThat().contains("Cannot set both exception and response"); + Truth.assertThat(expected) + .hasMessageThat() + .contains("Cannot set both exception and response"); } } @@ -121,7 +122,9 @@ public void testExceptionAndResponse() { issuer.setResponse(1); Assert.fail("BatchedFuture should have thrown an exception"); } catch (IllegalStateException expected) { - assertThat(expected).hasMessageThat().contains("Cannot set both exception and response"); + Truth.assertThat(expected) + .hasMessageThat() + .contains("Cannot set both exception and response"); } } } diff --git a/gax/src/test/java/com/google/api/gax/rpc/CancellationTest.java b/gax/src/test/java/com/google/api/gax/rpc/CancellationTest.java index 0d538577a..bb36d863c 100644 --- a/gax/src/test/java/com/google/api/gax/rpc/CancellationTest.java +++ b/gax/src/test/java/com/google/api/gax/rpc/CancellationTest.java @@ -29,8 +29,6 @@ */ package com.google.api.gax.rpc; -import static com.google.common.truth.Truth.assertThat; - import com.google.api.core.AbstractApiFuture; import com.google.api.core.ApiFuture; import com.google.api.core.SettableApiFuture; @@ -123,7 +121,7 @@ public void cancellationBeforeGetOnRetryingCallable() throws Exception { resultFuture.get(); Assert.fail("Callable should have thrown an exception"); } catch (CancellationException expected) { - assertThat(expected).hasMessageThat().contains("Task was cancelled"); + Truth.assertThat(expected).hasMessageThat().contains("Task was cancelled"); } } diff --git a/gax/src/test/java/com/google/api/gax/rpc/RetryingTest.java b/gax/src/test/java/com/google/api/gax/rpc/RetryingTest.java index c39872ad5..aaff28b97 100644 --- a/gax/src/test/java/com/google/api/gax/rpc/RetryingTest.java +++ b/gax/src/test/java/com/google/api/gax/rpc/RetryingTest.java @@ -29,8 +29,6 @@ */ package com.google.api.gax.rpc; -import static com.google.common.truth.Truth.assertThat; - import com.google.api.core.ApiFuture; import com.google.api.core.ApiFutures; import com.google.api.gax.core.FakeApiClock; @@ -43,6 +41,7 @@ import com.google.api.gax.rpc.testing.FakeStatusCode; import com.google.api.gax.rpc.testing.FakeTransportChannel; import com.google.common.collect.ImmutableSet; +import com.google.common.truth.Truth; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.UncheckedExecutionException; import org.junit.After; @@ -202,7 +201,7 @@ public void retryOnUnexpectedException() { assertRetrying(FAST_RETRY_SETTINGS); Assert.fail("Callable should have thrown an exception"); } catch (ApiException expected) { - assertThat(expected).isSameInstanceAs(throwable); + Truth.assertThat(expected).isSameInstanceAs(throwable); } } @@ -218,7 +217,7 @@ public void retryNoRecover() { assertRetrying(FAST_RETRY_SETTINGS); Assert.fail("Callable should have thrown an exception"); } catch (ApiException expected) { - assertThat(expected).isSameInstanceAs(throwable); + Truth.assertThat(expected).isSameInstanceAs(throwable); } } @@ -238,7 +237,7 @@ public void retryKeepFailing() { Futures.getUnchecked(future); Assert.fail("Callable should have thrown an exception"); } catch (UncheckedExecutionException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(throwable); + Truth.assertThat(expected).hasCauseThat().isSameInstanceAs(throwable); } } @@ -260,7 +259,7 @@ public void testKnownStatusCode() { callable.call(1); Assert.fail("Callable should have thrown an exception"); } catch (FailedPreconditionException expected) { - assertThat(expected.getMessage()).isEqualTo("known"); + Truth.assertThat(expected.getMessage()).isEqualTo("known"); } } @@ -279,7 +278,7 @@ public void testUnknownStatusCode() { callable.call(1); Assert.fail("Callable should have thrown an exception"); } catch (RuntimeException expected) { - assertThat(expected).isInstanceOf(RuntimeException.class); + Truth.assertThat(expected).isInstanceOf(RuntimeException.class); } } @@ -294,6 +293,6 @@ private void assertRetrying(RetrySettings retrySettings) { UnaryCallSettings callSettings = createSettings(retrySettings); UnaryCallable callable = FakeCallableFactory.createUnaryCallable(callInt, callSettings, clientContext); - assertThat(callable.call(1)).isEqualTo(2); + Truth.assertThat(callable.call(1)).isEqualTo(2); } } diff --git a/gax/src/test/java/com/google/api/gax/rpc/SpoolingCallableTest.java b/gax/src/test/java/com/google/api/gax/rpc/SpoolingCallableTest.java index cad95dee5..c968c2c87 100644 --- a/gax/src/test/java/com/google/api/gax/rpc/SpoolingCallableTest.java +++ b/gax/src/test/java/com/google/api/gax/rpc/SpoolingCallableTest.java @@ -29,8 +29,6 @@ */ package com.google.api.gax.rpc; -import static com.google.common.truth.Truth.assertThat; - import com.google.api.core.ApiFuture; import com.google.api.gax.rpc.testing.MockStreamingApi.MockServerStreamingCall; import com.google.api.gax.rpc.testing.MockStreamingApi.MockServerStreamingCallable; @@ -60,13 +58,13 @@ public void testHappyPath() throws InterruptedException, ExecutionException { ApiFuture> result = callable.futureCall("request"); MockServerStreamingCall call = upstream.popLastCall(); - assertThat(call.getController().isAutoFlowControlEnabled()).isTrue(); + Truth.assertThat(call.getController().isAutoFlowControlEnabled()).isTrue(); call.getController().getObserver().onResponse("response1"); call.getController().getObserver().onResponse("response2"); call.getController().getObserver().onComplete(); - assertThat(result.get()).containsExactly("response1", "response2").inOrder(); + Truth.assertThat(result.get()).containsExactly("response1", "response2").inOrder(); } @Test @@ -103,6 +101,6 @@ public void testNoResults() throws Exception { call.getController().getObserver().onComplete(); - assertThat(result.get()).isEmpty(); + Truth.assertThat(result.get()).isEmpty(); } }