4141import org .junit .AfterClass ;
4242import org .junit .Before ;
4343import org .junit .BeforeClass ;
44+ import org .junit .Rule ;
4445import org .junit .Test ;
46+ import org .junit .rules .ExpectedException ;
4547import org .junit .runner .RunWith ;
4648import 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