Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
Updated catch block with specific exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulKQL committed Feb 3, 2020
1 parent 7d10b30 commit 7694668
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 48 deletions.
Expand Up @@ -61,7 +61,7 @@ public void testNullToSelfWrongType() {
try {
GrpcCallContext.createDefault().nullToSelf(FakeCallContext.createDefault());
Assert.fail("GrpcCallContext should have thrown an exception");
} catch (Exception expected) {
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected)
.hasMessageThat()
.contains("context must be an instance of GrpcCallContext");
Expand Down Expand Up @@ -91,7 +91,7 @@ public void testWithTransportChannelWrongType() {
try {
GrpcCallContext.createDefault().withTransportChannel(FakeTransportChannel.create(channel));
Assert.fail("GrpcCallContext should have thrown an exception");
} catch (Exception expected) {
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected).hasMessageThat().contains("Expected GrpcTransportChannel");
}
}
Expand All @@ -101,7 +101,7 @@ public void testMergeWrongType() {
try {
GrpcCallContext.createDefault().merge(FakeCallContext.createDefault());
Assert.fail("GrpcCallContext should have thrown an exception");
} catch (Exception expected) {
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected)
.hasMessageThat()
.contains("context must be an instance of " + "GrpcCallContext");
Expand Down
Expand Up @@ -121,8 +121,10 @@ public void testBadContext() {
try {
streamingCallable.call(DEFAULT_REQUEST, observer);
Assert.fail("Callable should have thrown an exception");
} catch (Exception expected) {
Truth.assertThat(expected).isInstanceOf(IllegalArgumentException.class);
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected)
.hasMessageThat()
.contains("context must be an instance of GrpcCallContext");
}
}

Expand Down
Expand Up @@ -71,8 +71,10 @@ public void testResponseTransformer_exception() {
try {
transformer.apply(operationSnapshot);
Assert.fail("ResponseTransformer should have thrown an exception");
} catch (Exception expected) {
assertThat(expected).isInstanceOf(UnavailableException.class);
} catch (UnavailableException expected) {
assertThat(expected)
.hasMessageThat()
.contains("failed with status = GrpcStatusCode{transportCode=UNAVAILABLE}");
}
}

Expand All @@ -89,8 +91,7 @@ public void testResponseTransformer_mismatchedTypes() {
try {
transformer.apply(operationSnapshot);
Assert.fail("ResponseTransformer should have thrown an exception");
} catch (Exception expected) {
assertThat(expected).isInstanceOf(ApiException.class);
} catch (ApiException expected) {
assertThat(expected).hasMessageThat().contains("Failed to unpack object");
}
}
Expand Down Expand Up @@ -118,8 +119,7 @@ public void testMetadataTransformer_mismatchedTypes() {
try {
transformer.apply(operationSnapshot);
Assert.fail("MetadataTransformer should have thrown an exception");
} catch (Exception expected) {
assertThat(expected).isInstanceOf(ApiException.class);
} catch (ApiException expected) {
assertThat(expected).hasMessageThat().contains("Failed to unpack object");
}
}
Expand Down
Expand Up @@ -74,8 +74,7 @@ public void testResponseTransformer_exception() {
try {
transformer.apply(operationSnapshot);
Assert.fail("ResponseTransformer should have thrown an exception");
} catch (Exception expected) {
assertThat(expected).isInstanceOf(UnavailableException.class);
} catch (UnavailableException expected) {
assertThat(expected).hasMessageThat().contains("Unavailable; no response method");
}
}
Expand All @@ -92,8 +91,7 @@ public void testResponseTransformer_mismatchedTypes() {
try {
transformer.apply(operationSnapshot);
Assert.fail("ResponseTransformer should have thrown an exception");
} catch (Exception expected) {
assertThat(expected).isInstanceOf(ApiException.class);
} catch (ApiException expected) {
assertThat(expected).hasMessageThat().contains("cannot be cast");
}
}
Expand Down Expand Up @@ -122,8 +120,7 @@ public void testMetadataTransformer_mismatchedTypes() {
try {
transformer.apply(operationSnapshot);
Assert.fail("MetadataTransformer should have thrown an exception");
} catch (Exception expected) {
assertThat(expected).isInstanceOf(ApiException.class);
} catch (ApiException expected) {
assertThat(expected).hasMessageThat().contains("cannot be cast");
}
}
Expand Down
Expand Up @@ -50,8 +50,7 @@ public void testNullToSelfWrongType() {
try {
HttpJsonCallContext.createDefault().nullToSelf(FakeCallContext.createDefault());
Assert.fail("HttpJsonCallContext should have thrown an exception");
} catch (Exception expected) {
Truth.assertThat(expected).isInstanceOf(IllegalArgumentException.class);
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected)
.hasMessageThat()
.contains("context must be an instance of HttpJsonCallContext");
Expand Down Expand Up @@ -85,8 +84,7 @@ public void testWithTransportChannelWrongType() {
HttpJsonCallContext.createDefault()
.withTransportChannel(FakeTransportChannel.create(channel));
Assert.fail("HttpJsonCallContext should have thrown an exception");
} catch (Exception expected) {
Truth.assertThat(expected).isInstanceOf(IllegalArgumentException.class);
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected).hasMessageThat().contains("Expected HttpJsonTransportChannel");
}
}
Expand All @@ -96,8 +94,7 @@ public void testMergeWrongType() {
try {
HttpJsonCallContext.createDefault().merge(FakeCallContext.createDefault());
Assert.fail("HttpJsonCallContext should have thrown an exception");
} catch (Exception expected) {
Truth.assertThat(expected).isInstanceOf(IllegalArgumentException.class);
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected)
.hasMessageThat()
.contains("context must be an instance of HttpJsonCallContext");
Expand Down
Expand Up @@ -227,8 +227,7 @@ public void retryOnUnexpectedException() {
try {
callable.call(1);
Assert.fail("Callable should have thrown an exception");
} catch (Exception expected) {
assertThat(expected).isInstanceOf(ApiException.class);
} catch (ApiException expected) {
assertThat(expected).hasCauseThat().isSameInstanceAs(throwable);
}
}
Expand All @@ -255,8 +254,7 @@ public void retryNoRecover() {
try {
callable.call(1);
Assert.fail("Callable should have thrown an exception");
} catch (Exception expected) {
assertThat(expected).isInstanceOf(ApiException.class);
} catch (ApiException expected) {
assertThat(expected).isSameInstanceAs(apiException);
}
}
Expand Down Expand Up @@ -320,10 +318,10 @@ public void testKnownStatusCode() {
try {
callable.call(1);
Assert.fail("Callable should have thrown an exception");
} catch (FailedPreconditionException exception) {
assertThat(((HttpJsonStatusCode) exception.getStatusCode()).getTransportCode())
} catch (FailedPreconditionException expected) {
assertThat(((HttpJsonStatusCode) expected.getStatusCode()).getTransportCode())
.isEqualTo(STATUS_FAILED_PRECONDITION);
assertThat(exception.getMessage()).contains(HttpJsonStatusCode.FAILED_PRECONDITION);
assertThat(expected.getMessage()).contains(HttpJsonStatusCode.FAILED_PRECONDITION);
}
}

Expand All @@ -341,8 +339,8 @@ public void testUnknownStatusCode() {
try {
callable.call(1);
Assert.fail("Callable should have thrown an exception");
} catch (UnknownException exception) {
assertThat(exception.getMessage()).isEqualTo("java.lang.RuntimeException: unknown");
} catch (UnknownException expected) {
assertThat(expected.getMessage()).isEqualTo("java.lang.RuntimeException: unknown");
}
}

Expand Down
Expand Up @@ -226,7 +226,7 @@ public void testExceptionWithNullFlowController() {
.setBatchMerger(new SimpleBatchMerger())
.build();
Assert.fail("ThresholdBatcher should have thrown an exception");
} catch (Exception expected) {
} catch (NullPointerException expected) {
assertThat(expected).isInstanceOf(NullPointerException.class);
}
}
Expand Down
Expand Up @@ -60,7 +60,7 @@ public void throwsApiException() {
try {
ApiExceptions.callAndTranslateApiException(ApiFutures.immediateFailedFuture(throwable));
Assert.fail("ApiExceptions should have thrown an exception");
} catch (Exception expected) {
} catch (ApiException expected) {
assertThat(expected).isSameInstanceAs(throwable);
}
}
Expand Down
Expand Up @@ -90,8 +90,10 @@ public void testNoResult() {
BatchedRequestIssuer<Integer> issuer = new BatchedRequestIssuer<>(batchedFuture, 2);
issuer.sendResult();
Assert.fail("BatchedFuture should have thrown an exception");
} catch (Exception expected) {
assertThat(expected).isInstanceOf(IllegalStateException.class);
} catch (IllegalStateException expected) {
assertThat(expected)
.hasMessageThat()
.contains("Neither response nor exception were set in BatchedRequestIssuer");
}
}

Expand All @@ -104,8 +106,8 @@ public void testResponseAndException() {
issuer.setResponse(1);
issuer.setException(thrownException);
Assert.fail("BatchedFuture should have thrown an exception");
} catch (Exception expected) {
assertThat(expected).isInstanceOf(IllegalStateException.class);
} catch (IllegalStateException expected) {
assertThat(expected).hasMessageThat().contains("Cannot set both exception and response");
}
}

Expand All @@ -118,8 +120,8 @@ public void testExceptionAndResponse() {
issuer.setException(thrownException);
issuer.setResponse(1);
Assert.fail("BatchedFuture should have thrown an exception");
} catch (Exception expected) {
assertThat(expected).isInstanceOf(IllegalStateException.class);
} catch (IllegalStateException expected) {
assertThat(expected).hasMessageThat().contains("Cannot set both exception and response");
}
}
}
Expand Up @@ -122,8 +122,8 @@ public void cancellationBeforeGetOnRetryingCallable() throws Exception {
resultFuture.cancel(true);
resultFuture.get();
Assert.fail("Callable should have thrown an exception");
} catch (Exception expected) {
assertThat(expected).isInstanceOf(CancellationException.class);
} catch (CancellationException expected) {
assertThat(expected).hasMessageThat().contains("Task was cancelled");
}
}

Expand Down
8 changes: 4 additions & 4 deletions gax/src/test/java/com/google/api/gax/rpc/RetryingTest.java
Expand Up @@ -200,7 +200,7 @@ public void retryOnUnexpectedException() {
try {
assertRetrying(FAST_RETRY_SETTINGS);
Assert.fail("Callable should have thrown an exception");
} catch (Exception expected) {
} catch (ApiException expected) {
assertThat(expected).isSameInstanceAs(throwable);
}
}
Expand All @@ -216,7 +216,7 @@ public void retryNoRecover() {
try {
assertRetrying(FAST_RETRY_SETTINGS);
Assert.fail("Callable should have thrown an exception");
} catch (Exception expected) {
} catch (ApiException expected) {
assertThat(expected).isSameInstanceAs(throwable);
}
}
Expand Down Expand Up @@ -258,8 +258,8 @@ public void testKnownStatusCode() {
try {
callable.call(1);
Assert.fail("Callable should have thrown an exception");
} catch (FailedPreconditionException exception) {
assertThat(exception.getMessage()).isEqualTo("known");
} catch (FailedPreconditionException expected) {
assertThat(expected.getMessage()).isEqualTo("known");
}
}

Expand Down
Expand Up @@ -91,8 +91,7 @@ public void testEarlyTermination() throws Exception {
try {
result.get();
Assert.fail("Callable should have thrown an exception");
} catch (Exception expected) {
Truth.assertThat(expected).isInstanceOf(CancellationException.class);
} catch (CancellationException expected) {
Truth.assertThat(expected).hasMessageThat().contains("Task was cancelled.");
}
}
Expand Down

0 comments on commit 7694668

Please sign in to comment.