This library provides tools to help write tests for code that uses gcloud-java services.
You can test against a temporary local datastore by following these steps:
- Start the local datastore emulator using
LocalGcdHelper
. This can be done in two ways:
- Run
LocalGcdHelper.java
'smain
method with argumentsSTART
and (optionally)--port=<port number>
. This will create a temporary folder on your computer and bindlocalhost:<port number>
for communication with the local datastore. The port number is an optional argument. If no port number is specified, port 8080 will be used. - Call
LocalGcdHelper.start(<project ID>, <port number>)
before running your tests. Save theLocalGcdHelper
object returned so that you can stop the emulator later.
- In your program, create and use a datastore whose host is set host to
localhost:<port number>
. For example,
DatastoreOptions options = DatastoreOptions.builder()
.projectId(PROJECT_ID)
.host("http://localhost:8080")
.build();
Datastore localDatastore = DatastoreFactory.instance().get(options);
-
Run your tests.
-
Stop the local datastore emulator.
- If you ran
LocalGcdHelper.java
'smain
function to start the emulator, runLocalGcdHelper.java
'smain
method with argumentsSTOP
and (optionally)--port=<port number>
. If the port is not supplied, the program will attempt to close the last port started. - If you ran
LocalGcdHelper.start()
to start the emulator, call thestop()
method on theLocalGcdHelper
object returned byLocalGcdHelper.start()
.
You can test against a remote datastore emulator as well. To do this, set the DatastoreOptions
project endpoint to the hostname of the remote machine, like the example below.
DatastoreOptions options = DatastoreOptions.builder()
.projectId(PROJECT_ID)
.host("http://<hostname of machine>:<port>")
.build();
Datastore localDatastore = DatastoreFactory.instance().get(options);
Note that the remote datastore must be running before your tests are run.
Currently, there isn't an emulator for Google Cloud Storage, so an alternative is to create a test project. RemoteGcsHelper
contains convenience methods to make setting up and cleaning up the test project easier. To use this class, follow the steps below:
-
Create a test Google Cloud project.
-
Download a JSON service account credentials file from the Google Developer's Console. See more about this on the Google Cloud Platform Storage Authentication page.
-
Create a
RemoteGcsHelper
object using your project ID and JSON key. Here is an example that uses theRemoteGcsHelper
to create a bucket.
RemoteGcsHelper gcsHelper = RemoteGcsHelper.create(PROJECT_ID, "/path/to/my/JSON/key.json");
Storage storage = StorageFactory.instance().get(gcsHelper.options());
String bucket = RemoteGcsHelper.generateBucketName();
storage.create(BucketInfo.of(bucket));
-
Run your tests.
-
Clean up the test project by using
forceDelete
to clear any buckets used. Here is an example that clears the bucket created in Step 3 with a timeout of 5 seconds.
RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);