public class DatastoreEmulator extends Datastore
Datastore that provides lifecycle management for a datastore emulator.
In order to use the emulator for a JUnit 4 test you might do something like this:
public class MyTest {
static DatastoreEmulator datastore;
@BeforeClass
public static void startEmulator() throws DatastoreEmulatorException {
DatastoreOptions options = new DatastoreOptions.Builder()
.localHost("localhost:8080")
.projectId("my-project-id")
.build();
datastore = DatastoreEmulatorFactory.get().create(options);
datastore.start("/usr/local/cloud-datastore-emulator", "my-project-id");
}
@Before
public void setUp() throws DatastoreEmulatorException {
datastore.clear();
}
@AfterClass
public static void stopEmulator() throws DatastoreEmulatorException {
datastore.stop();
}
@Test
public void testFoo1() { }
@Test
public void testFoo2() { }
}
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Clears all data in the emulator.
|
File |
getProjectDirectory() |
void |
start(String emulatorDir,
String projectId,
String... commandLineOptions)
Starts the emulator.
|
void |
stop()
Stops the emulator.
|
allocateIds, beginTransaction, commit, getRpcCount, lookup, resetRpcCount, rollback, runQuerypublic void clear()
throws DatastoreEmulatorException
DatastoreEmulatorException - if the clear failspublic void start(String emulatorDir, String projectId, String... commandLineOptions) throws DatastoreEmulatorException
stop(). Note that
receiving an exception does not indicate that the server did not start. We recommend calling
stop() to ensure the server is not running regardless of the result of this method.emulatorDir - The path to the emulator directory, e.g. /usr/local/cloud-datastore-emulatorprojectId - The project IDcommandLineOptions - Command line options to pass to the emulator on startupDatastoreEmulatorException - If start(java.lang.String, java.lang.String, java.lang.String...) has already been called or the server does
not start successfully.public void stop()
throws DatastoreEmulatorException
DatastoreEmulatorException - if the emulator cannot be stoppedpublic File getProjectDirectory()
Copyright © 2017. All rights reserved.