31

For the purpose of testing, I would like to mock Cloud Storage because it slows tests down.

Is there Google Cloud Storage emulator?

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
Evgeny Timoshenko
  • 3,119
  • 5
  • 33
  • 53

2 Answers2

42

Google has an in-memory emulator you can use (majority of core functions are implemented).

You need com.google.cloud:google-cloud-nio on your test classpath (:0.25.0-alpha currently). Then you can use/inject Storage interface implemented by the in-memory LocalStorageHelper test-helper service.

Example usage:

  import com.google.cloud.storage.Storage;
  import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper;

  @Test
  public void exampleInMemoryGoogleStorageTest() {
    Storage storage = LocalStorageHelper.getOptions().getService();

    final String blobPath = "test/path/foo.txt";
    final String testBucketName = "test-bucket";
    BlobInfo blobInfo = BlobInfo.newBuilder(
        BlobId.of(testBucketName, blobPath)
    ).build();

    storage.create(blobInfo, "randomContent".getBytes(StandardCharsets.UTF_8));
    Iterable<Blob> allBlobsIter = storage.list(testBucketName).getValues();
    // expect to find the blob we saved when iterating over bucket blobs
    assertTrue(
        StreamSupport.stream(allBlobsIter.spliterator(), false)
            .map(BlobInfo::getName)
            .anyMatch(blobPath::equals)
    );
  }
Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161
ThomasMH
  • 638
  • 1
  • 7
  • 8
15

There isn't an official emulator provided by Google for the time being.

I'm currently using project Minio (https://www.minio.io/) for mocking Google Storage's behavior in development (Minio uses the filesystem as storage backend and provides compatibility with S3 apiV2, which is compatible with Google Storage).

Caio Tomazelli
  • 513
  • 5
  • 14
  • 2
    Can you provide some details on the setup please? – 21st Aug 01 '20 at 11:54
  • I would also be happy if you can provide some details. Are you able to create a Storage/Bucket reference that points to the local MinIO instance? Or you just mock out the service which calls the Cloud Storage APIs and you store the files via the mock service? – Dominik Aug 28 '20 at 11:23
  • 1
    I don't have access to the project setup anymore, but since I answered this questions Google has developed an emulator for Storage: https://github.com/googleapis/google-cloud-java/blob/master/TESTING.md#testing-code-that-uses-storage – Caio Tomazelli Sep 24 '20 at 00:49
  • That emulator seems to be just a java class implementing the cloud storage interface of the java client, so it's really just dedicated to java developers – gsouf Mar 11 '21 at 15:54
  • The file mentioned by Caio was deleted, the latest version is: https://github.com/googleapis/google-cloud-java/blob/a29a7cf422a2263b796bc21b2304701d322f0217/TESTING.md#testing-code-that-uses-storage – Christophe Moine Sep 14 '22 at 12:10