This library provides tools to help write tests for code that uses the following google-cloud services:
Currently, there isn't an emulator for Google BigQuery, so an alternative is to create a test
project. RemoteBigQueryHelper
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.
-
Create a
RemoteBigQueryHelper
object using your project ID and JSON key. Here is an example that uses theRemoteBigQueryHelper
to create a dataset.
RemoteBigQueryHelper bigqueryHelper =
RemoteBigQueryHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
BigQuery bigquery = bigqueryHelper.getOptions().getService();
String dataset = RemoteBigQueryHelper.generateDatasetName();
bigquery.create(DatasetInfo.newBuilder(dataset).build());
-
Run your tests.
-
Clean up the test project by using
forceDelete
to clear any datasets used. Here is an example that clears the dataset created in Step 3.
RemoteBigQueryHelper.forceDelete(bigquery, dataset);
Bigtable integration tests can either be run against an emulator or a real Bigtable table. The
target environment can be selected by setting a maven profile. By default it is set to
bigtable-emulator-it
and other options are bigtable-prod-it
and bigtable-directpath-it
.
To use the bigtable-prod-it
and bigtable-directpath-it
environments:
- Set up the target table using
google-cloud-clients/google-cloud-bigtable/scripts/setup-test-table.sh
- Download the JSON service account credentials file from the Google Developer's Console.
- Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the credentials file - Enable the profile and the system properties
bigtable.project
,bigtable.instance
andbigtable.table
to created earlier. Example:mvn verify -am -pl google-cloud-clients/google-cloud-bigtable \ -P bigtable-prod-it \ -Dbigtable.project=my-project -Dbigtable.instance=my-instance -Dbigtable.table=my-table
Currently, there isn't an emulator for Google Compute, so an alternative is to create a test
project. RemoteComputeHelper
contains convenience methods to make setting 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.
-
Create a
RemoteComputeHelper
object using your project ID and JSON key. Here is an example that uses theRemoteComputeHelper
to create an address.
RemoteComputeHelper computeHelper =
RemoteBigQueryHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
Compute compute = computeHelper.getOptions().getService();
// Pick a name for the resource with low probability of clashing
String addressName = RemoteComputeHelper.baseResourceName() + "address";
AddressId addressId = RegionAddressId.of(REGION, addressName);
AddressInfo addressInfo = AddressInfo.of(addressId);
Operation operation = compute.create(addressInfo);
- Run your tests.
You can test against a temporary local Datastore by following these steps:
-
Start the emulator
$ gcloud beta emulators datastore start
To determine which host/port the emulator is running on:
$ gcloud beta emulators datastore env-init
# Sample output:
# export DATASTORE_EMULATOR_HOST=localhost:8759
- Point your client to the emulator
DatastoreOptions options = DatastoreOptions.newBuilder()
.setProjectId(DatastoreOptions.getDefaultProjectId())
.setHost(System.getenv("DATASTORE_EMULATOR_HOST"))
.setCredentials(NoCredentials.getInstance())
.setRetrySettings(ServiceOptions.getNoRetrySettings())
.build();
Datastore datastore = options.getService();
- Run your tests
Currently, there isn't an emulator for DNS. An alternative is to create a test project.
Currently, there isn't an emulator for Stackdriver Logging, so an alternative is to create a test
project. RemoteLoggingHelper
contains convenience methods to make setting 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.
-
Create a
RemoteLoggingHelper
object using your project ID and JSON key. Here is an example that uses theRemoteLoggingHelper
to create a metric.
RemoteLoggingHelper loggingHelper =
RemoteLoggingHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
Logging logging = loggingHelper.getOptions().getService();
// Pick a name for the resource with low probability of clashing
String metricName = RemoteLoggingHelper.formatForTest("test-metric");
MetricInfo metricInfo = MetricInfo.of(name, "logName:syslog");
Metric metric = logging.create(metricInfo);
- Run your tests.
You can test against a Pub/Sub emulator:
Currently, there isn't an emulator for Redis. An alternative is to create a test project.
You can test against an in-memory local Resource Manager by following these steps:
- Before running your testing code, start the Resource Manager emulator
LocalResourceManagerHelper
. This can be done as follows:
LocalResourceManagerHelper helper = LocalResourceManagerHelper.create();
helper.start();
This will spawn a server thread that listens to localhost
at an ephemeral port for Resource Manager requests.
- In your program, create and use a Resource Manager service object whose host is set to
localhost
at the appropriate port. For example:
ResourceManager resourceManager = LocalResourceManagerHelper.getOptions().getService();
-
Run your tests.
-
Stop the Resource Manager emulator.
helper.stop();
This method will block until the server thread has been terminated.
You can test against an in-memory local Storage. The in-memory configuration supports only limited number of operations; please refer to the LocalStorageHelper
class documentation for more details. Please use RemoteStorageHelper
(see next section) if you need to use operations which are not supported by LocalStorageHelper
.
To use the in-memory configuration please follow these steps:
- Follow the Quickstart instructions to add the nio dependency to your project.
- In your program, create and use a fake Storage service object. For example:
Storage storage = LocalStorageHelper.getOptions().getService();
- Run your tests.
The alternative way of testing is to create a test project. This way allows using operations not supported by the in-memory configuration. RemoteStorageHelper
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
RemoteStorageHelper
object using your project ID and JSON key. Here is an example that uses theRemoteStorageHelper
to create a bucket.
RemoteStorageHelper helper =
RemoteStorageHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
Storage storage = helper.getOptions().getService();
String bucket = RemoteStorageHelper.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.
RemoteStorageHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);
RemoteTranslateHelper
contains convenience methods to make is easier to run tests against the
Google Translation service.
-
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 Authentication page key.
-
Create a
RemoteTranslateHelper
object using your project ID and API key. Here is an example that uses theRemoteTranslateHelper
to list supported languages.
RemoteTranslateHelper translateHelper =
RemoteTranslateHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
Translate translate = translateHelper.getOptions().getService();
List<Language> languages = translate.listSupportedLanguages();
- Run your tests.
Currently, there isn't an emulator for Cloud Spanner, so an alternative is to create a test project. RemoteSpannerHelper
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 Authentication page.
-
Create or use an existing Cloud Spanner Instance.
-
Create a
RemoteSpannerHelper
object using your instance ID andSpannerOptions
pointing to the credentials file. Here is an example that uses theRemoteSpannerHelper
to create a database.
SpannerOptions options = SpannerOptions.newBuilder()
.setCredentials(GoogleCredentials.fromStream(new FileInputStream("/path/to/my/JSON/key.json")))
.build()
RemoteSpannerHelper helper =
RemoteSpannerHelper.create(options, InstanceId.of(options.getProjectId(), INSTANCE_ID),
new FileInputStream("/path/to/my/JSON/key.json"));
Database db = RemoteSpannerHelper.createTestDatabase("my ddl statements"...);
DatabaseClient client = RemoteSpannerHelper.getDatabaseClient(db);
-
Run your tests.
-
Clean up the test project by using
cleanUp
to clear any databases created.
RemoteSpannerHelper.cleanUp();