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

Commit

Permalink
test: cleaning up deprecated usages (#858)
Browse files Browse the repository at this point in the history
* test: cleaning up deprecated usages

removed deprecated `ExpectedException.none()` usage across unit test cases.

chore: address feedback comments

* chore: removed Truth.assertThat as static import
  • Loading branch information
rahulKQL committed Apr 10, 2020
1 parent a8827ad commit b988b95
Show file tree
Hide file tree
Showing 17 changed files with 261 additions and 194 deletions.
Expand Up @@ -46,22 +46,26 @@
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;
import org.threeten.bp.Duration;

@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
Expand All @@ -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
Expand Down
Expand Up @@ -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;

Expand All @@ -79,8 +78,6 @@ public class GrpcDirectServerStreamingCallableTest {
private ServerStreamingCallSettings<Color, Money> streamingCallSettings;
private ServerStreamingCallable<Color, Money> streamingCallable;

@Rule public ExpectedException thrown = ExpectedException.none();

@Before
public void setUp() throws InstantiationException, IllegalAccessException, IOException {
String serverName = "fakeservice";
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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;

Expand All @@ -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";
Expand Down
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
Expand Up @@ -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() {
Expand All @@ -63,30 +61,38 @@ public void testResponseTransformer() {

@Test
public void testResponseTransformer_exception() {
thrown.expect(UnavailableException.class);
ResponseTransformer<Money> 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<Money> 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(
Operation.newBuilder()
.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
Expand All @@ -101,17 +107,19 @@ public void testMetadataTransformer() {

@Test
public void testMetadataTransformer_mismatchedTypes() {
thrown.expect(ApiException.class);
thrown.expectMessage("Failed to unpack object");
MetadataTransformer<Money> 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(
Operation.newBuilder()
.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");
}
}
}
Expand Up @@ -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;
Expand All @@ -68,8 +66,6 @@
@RunWith(JUnit4.class)
public class SettingsTest {

@Rule public ExpectedException thrown = ExpectedException.none();

interface FakePagedListResponse extends PagedListResponse<Integer> {}

private static class FakeStubSettings extends StubSettings<FakeStubSettings> {
Expand Down
Expand Up @@ -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() {
Expand All @@ -66,30 +64,35 @@ public void testResponseTransformer() {

@Test
public void testResponseTransformer_exception() {
thrown.expect(UnavailableException.class);
ResponseTransformer<EmptyMessage> 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<EmptyMessage> transformer = ResponseTransformer.create(EmptyMessage.class);
FakeMetadataMessage metadata = new FakeMetadataMessage(Status.PENDING, Code.OK);
ApiMessage bananaResponse =
new FakeApiMessage(ImmutableMap.<String, Object>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
Expand All @@ -105,8 +108,6 @@ public void testMetadataTransformer() {

@Test
public void testMetadataTransformer_mismatchedTypes() {
thrown.expect(ApiException.class);
thrown.expectMessage("cannot be cast");
MetadataTransformer<FakeOperationMessage> transformer =
MetadataTransformer.create(FakeOperationMessage.class);
FakeMetadataMessage metadataMessage = new FakeMetadataMessage(Status.PENDING, Code.OK);
Expand All @@ -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 {
Expand Down
Expand Up @@ -35,22 +35,26 @@
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;
import org.threeten.bp.Duration;

@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
Expand All @@ -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
Expand Down

0 comments on commit b988b95

Please sign in to comment.