Jon Newmuis
Jon Newmuis

Reputation: 26502

How to unit test an application using Google Drive API (Java client)

What is the best way to unit test an application using the Google Drive API (Java client)?

It seems like applications written rely heavily on the Drive class, but short of either...

...how could such an application be tested?

Using mock frameworks like Mockito are a bit tedious with the Drive API (Java client), since usage of the Drive Java client rely on making so many chained calls (e.g., from the documentation):

Drive service = getDriveService(req, resp);
service.files().get(fileId).execute();

Upvotes: 12

Views: 4437

Answers (1)

Adrian Shum
Adrian Shum

Reputation: 40036

It shouldn't be that tedious in Mockito in fact, with the help of deep stub:

Drive mockDrive = mock(Drive.class, RETURNS_DEEP_STUBS);

....
// stubbing
when(service.files().get(anyString()).execute()).thenReturn(something);

// verify
verify(service.files().get("Some Field ID").execute();

Learn more from documentation of Mockito

It is fine if you write integration test to test against the actual Drive service, but it simply cannot replace unit testing.

Upvotes: 4

Related Questions