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..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 @@ -41,15 +41,13 @@ 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() { @@ -63,22 +61,25 @@ public void testResponseTransformer() { @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) { + Truth.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 +87,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) { + Truth.assertThat(expected).hasMessageThat().contains("Failed to unpack object"); + } } @Test @@ -101,10 +107,7 @@ public void testMetadataTransformer() { @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 +115,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) { + Truth.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..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 @@ -40,16 +40,14 @@ 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() { @@ -66,30 +64,35 @@ public void testResponseTransformer() { @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) { + Truth.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) { + Truth.assertThat(expected).hasMessageThat().contains("cannot be cast"); + } } @Test @@ -105,8 +108,6 @@ public void testMetadataTransformer() { @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 +116,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) { + Truth.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..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 @@ -53,10 +53,9 @@ 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 +112,6 @@ public void teardown() { executor.shutdownNow(); } - @Rule public ExpectedException thrown = ExpectedException.none(); - static ApiFuture immediateFailedFuture(Throwable t) { return ApiFutures.immediateFailedFuture(t); } @@ -219,8 +216,6 @@ public void retryOnStatusUnknown() { @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 +224,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) { + Truth.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 +251,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) { + Truth.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) { + Truth.assertThat(expected).hasCauseThat().isInstanceOf(ApiException.class); + Truth.assertThat(expected).hasCauseThat().hasMessageThat().contains("Unavailable"); + } } @Test @@ -311,10 +317,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) { + Truth.assertThat(((HttpJsonStatusCode) expected.getStatusCode()).getTransportCode()) .isEqualTo(STATUS_FAILED_PRECONDITION); - Truth.assertThat(exception.getMessage()).contains(HttpJsonStatusCode.FAILED_PRECONDITION); + Truth.assertThat(expected.getMessage()).contains(HttpJsonStatusCode.FAILED_PRECONDITION); } } @@ -331,8 +338,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) { + Truth.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..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 @@ -32,14 +32,10 @@ import com.google.common.truth.Truth; 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(); @@ -88,32 +84,47 @@ public void testException() throws Exception { @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) { + Truth.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) { + Truth.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) { + 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 55c974109..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 @@ -47,10 +47,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 +87,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 +107,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) { + Truth.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..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 @@ -45,10 +45,9 @@ 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 +92,6 @@ public void teardown() { executor.shutdownNow(); } - @Rule public ExpectedException thrown = ExpectedException.none(); - static ApiFuture immediateFailedFuture(Throwable t) { return ApiFutures.immediateFailedFuture(t); } @@ -196,43 +193,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) { + Truth.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) { + Truth.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) { + Truth.assertThat(expected).hasCauseThat().isSameInstanceAs(throwable); + } } @Test @@ -251,14 +257,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) { + Truth.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 +274,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) { + Truth.assertThat(expected).isInstanceOf(RuntimeException.class); + } } public static UnaryCallSettings createSettings(RetrySettings retrySettings) { 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..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; @@ -38,10 +36,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 +47,6 @@ public class SpoolingCallableTest { private MockServerStreamingCallable upstream; private SpoolingCallable callable; - @Rule public ExpectedException expectedException = ExpectedException.none(); - @Before public void setup() { upstream = new MockServerStreamingCallable<>(); @@ -63,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 @@ -91,8 +86,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 @@ -102,6 +101,6 @@ public void testNoResults() throws Exception { call.getController().getObserver().onComplete(); - assertThat(result.get()).isEmpty(); + Truth.assertThat(result.get()).isEmpty(); } } 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 {