3

I write a lot of apps that end up integrating with Google Drive for one reason or another. It's such a useful cloud based storage utility that it's got integrations all over the place.

So many of my integration tests are needing to call the actual Google API. Such a waste.

You can use Mockito to mock it, but that's a ton of work. And it's only for unit testing that has stubbed out mocks.

Has anyone made a mock HTTP web service that can mock the Google drive api?

I was hoping to find someone who created a Docker container that fires up a mock Google drive api that you can point the Google-drive-sdk to and have it work from a linux filesystem backend.

S3 has this. Example: https://hub.docker.com/r/lphoward/fake-s3/

I'm pretty sure no such docker container exists. Is there there anything in the makes at Google? Thanks for your time!

Nicholas DiPiazza
  • 10,029
  • 11
  • 83
  • 152

2 Answers2

1

Here's what I have found:

For JAVA:

Please try checking HTTP Unit Testing. As mentioned in the documentation:

When writing unit tests using this HTTP framework, don't make requests to a real server. Instead, mock the HTTP transport and inject fake HTTP requests and responses. The pluggable HTTP transport layer of the Google HTTP Client Library for Java makes this flexible and simple to do.

Also, some useful testing utilities are included in the com.google.api.client.testing.http package (@Beta).

For Android:

If you're using Android Studio, the location of your test code depends on the type of test you are writing. Android Studio provides source code directories (source sets).

  • Local unit tests

    Located at module-name/src/test/java/.

    These are tests that run on your machine's local Java Virtual Machine (JVM). Use these tests to minimize execution time when your tests have no Android framework dependencies or when you can mock the Android framework dependencies.

    At runtime, these tests are executed against a modified version of android.jar where all final modifiers have been stripped off. This lets you use popular mocking libraries, like Mockito.

You may want check this SO post and see if it helps.

For Python:

You may try Mocks.

The use of Mock objects is a standard testing methodology for Python and other object-oriented languages. This library defines Mock classes that simulate responses to API calls. You can use them to test how your code handles basic interactions with Google APIs.

You can use the HttpMock class which simulates the response to a single HTTP request.

Hope that helps!

Community
  • 1
  • 1
Teyam
  • 7,686
  • 3
  • 15
  • 22
  • The link about the Google testing package is dead. Documentation can be found here: https://cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.testing.http.apache However, it doesn't provide testing for Google Drive API. – Martina Dec 29 '22 at 14:18
1

For python, if you are brave, you could use vcrpy as gcsfs does. It records all requests and responses over HTTP into YAML files, which you can then test against, with complex matching (so that, for instance, etags and timestamps don't matter) and replacement rules (so that secure data isn't leaked into version control).

mdurant
  • 27,272
  • 5
  • 45
  • 74
  • already tried this with wiremock. very problematic due to all the weird JWT security handshakes and it would be a massive effort. And also, you'd really need to do a lot of work tricking it into thinking you had 10,000's of users who each own lots of files. – Nicholas DiPiazza Oct 21 '19 at 17:33