GCloud Module

Note

This module is INCUBATING. While it is ready for use and operational in the current version of Testcontainers, it is possible that it may receive breaking changes in the future. See our contributing guidelines for more information on our incubating modules policy.

Testcontainers module for the Google's Cloud SDK.

Currently, the module supports Datastore, Firestore, Pub/Sub and Spanner emulators. In order to use it, you should use the following classes:

  • DatastoreEmulatorContainer
  • FirestoreEmulatorContainer
  • PubSubEmulatorContainer
  • SpannerEmulatorContainer

Usage example

Running GCloud as a stand-in for Google Datastore during a test:

public DatastoreEmulatorContainer emulator = new DatastoreEmulatorContainer(
    DockerImageName.parse("gcr.io/google.com/cloudsdktool/cloud-sdk:313.0.0")
);

And how to start it:

@Test
public void testSimple() {
    DatastoreOptions options = DatastoreOptions.newBuilder()
            .setHost(emulator.getEmulatorEndpoint())
            .setCredentials(NoCredentials.getInstance())
            .setRetrySettings(ServiceOptions.getNoRetrySettings())
            .setProjectId("test-project")
            .build();
    Datastore datastore = options.getService();

    Key key = datastore.newKeyFactory().setKind("Task").newKey("sample");
    Entity entity = Entity.newBuilder(key).set("description", "my description").build();
    datastore.put(entity);

    assertThat(datastore.get(key).getString("description")).isEqualTo("my description");
}

Adding this module to your project dependencies

Add the following dependency to your pom.xml/build.gradle file:

testCompile "org.testcontainers:gcloud:1.15.0-rc2"
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>gcloud</artifactId>
    <version>1.15.0-rc2</version>
    <scope>test</scope>
</dependency>