Skip to content

Commit 8928c49

Browse files
author
Ajay Kannan
committed
Add test for runtime exception
1 parent abb421e commit 8928c49

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
import org.junit.AfterClass;
4242
import org.junit.Before;
4343
import org.junit.BeforeClass;
44+
import org.junit.Rule;
4445
import org.junit.Test;
46+
import org.junit.rules.ExpectedException;
4547
import org.junit.runner.RunWith;
4648
import org.junit.runners.JUnit4;
4749

@@ -102,6 +104,9 @@ public class DatastoreTest {
102104

103105
private static LocalGcdHelper gcdHelper;
104106

107+
@Rule
108+
public ExpectedException thrown = ExpectedException.none();
109+
105110
@BeforeClass
106111
public static void beforeClass() throws IOException, InterruptedException {
107112
if (!LocalGcdHelper.isActive(PROJECT_ID)) {
@@ -635,7 +640,7 @@ public void testKeyFactory() {
635640
}
636641

637642
@Test
638-
public void testRetires() throws Exception {
643+
public void testRetries() throws Exception {
639644
DatastoreV1.LookupRequest requestPb =
640645
DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build();
641646
DatastoreV1.LookupResponse responsePb = DatastoreV1.LookupResponse.newBuilder()
@@ -657,4 +662,27 @@ public void testRetires() throws Exception {
657662
assertEquals(ENTITY1, entity);
658663
EasyMock.verify(rpcFactoryMock, rpcMock);
659664
}
665+
666+
@Test
667+
public void testRuntimeExceptionHandling() throws Exception {
668+
DatastoreV1.LookupRequest requestPb =
669+
DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build();
670+
DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class);
671+
DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class);
672+
EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class)))
673+
.andReturn(rpcMock);
674+
String exceptionMessage = "Artificial runtime exception";
675+
EasyMock.expect(rpcMock.lookup(requestPb))
676+
.andThrow(new RuntimeException(exceptionMessage));
677+
EasyMock.replay(rpcFactoryMock, rpcMock);
678+
DatastoreOptions options = this.options.toBuilder()
679+
.retryParams(RetryParams.getDefaultInstance())
680+
.serviceRpcFactory(rpcFactoryMock)
681+
.build();
682+
Datastore datastore = DatastoreFactory.instance().get(options);
683+
thrown.expect(DatastoreException.class);
684+
thrown.expectMessage(exceptionMessage);
685+
datastore.get(KEY1);
686+
EasyMock.verify(rpcFactoryMock, rpcMock);
687+
}
660688
}

0 commit comments

Comments
 (0)