Skip to content

Commit

Permalink
test: switch to assertThrows to capture exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumin8 committed May 9, 2024
1 parent bc0cf7c commit 64ac5a7
Showing 1 changed file with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.showcase.v1beta1.it;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.api.gax.httpjson.*;
import com.google.api.gax.rpc.ApiClientHeaderProvider;
Expand Down Expand Up @@ -46,6 +47,9 @@ public class ITApiVersionHeaders {

private static final String EXPECTED_ECHO_API_VERSION = "v1_20240408";
private static final String CUSTOM_API_VERSION = "user-supplied-version";
private static final String EXPECTED_EXCEPTION_MESSAGE =
"Header provider can't override the header: "
+ ApiClientHeaderProvider.API_VERSION_HEADER_KEY;

// Implement a client interceptor to retrieve the trailing metadata from response.
private static class GrpcCapturingClientInterceptor implements ClientInterceptor {
Expand Down Expand Up @@ -218,7 +222,7 @@ public void testHttpJson_noApiVersion() {
.isNotIn(httpJsonComplianceInterceptor.metadata.getHeaders().keySet());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testGrpcEcho_userApiVersionThrowsException() throws IOException {
StubSettings stubSettings =
grpcClient
Expand All @@ -230,13 +234,14 @@ public void testGrpcEcho_userApiVersionThrowsException() throws IOException {
ApiClientHeaderProvider.API_VERSION_HEADER_KEY, CUSTOM_API_VERSION))
.build();

try (EchoClient echo =
EchoClient.create(EchoSettings.create((EchoStubSettings) stubSettings))) {
assertThat(true).isFalse();
}
IllegalArgumentException exception =
assertThrows(
IllegalArgumentException.class,
() -> EchoClient.create(EchoSettings.create((EchoStubSettings) stubSettings)));
assertThat(exception.getMessage()).isEqualTo(EXPECTED_EXCEPTION_MESSAGE);
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testHttpJsonEcho_userApiVersionThrowsException() throws IOException {
StubSettings stubSettings =
httpJsonClient
Expand All @@ -248,10 +253,11 @@ public void testHttpJsonEcho_userApiVersionThrowsException() throws IOException
ApiClientHeaderProvider.API_VERSION_HEADER_KEY, CUSTOM_API_VERSION))
.build();

try (EchoClient echo =
EchoClient.create(EchoSettings.create((EchoStubSettings) stubSettings))) {
assertThat(true).isFalse();
}
IllegalArgumentException exception =
assertThrows(
IllegalArgumentException.class,
() -> EchoClient.create(EchoSettings.create((EchoStubSettings) stubSettings)));
assertThat(exception.getMessage()).isEqualTo(EXPECTED_EXCEPTION_MESSAGE);
}

@Test
Expand Down

0 comments on commit 64ac5a7

Please sign in to comment.