Class DatastoreEmulator (2.17.6)

public class DatastoreEmulator extends Datastore

An extension to 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() { }

}

Inheritance

java.lang.Object > Datastore > DatastoreEmulator

Methods

clear()

public void clear()

Clears all data in the emulator.

Exceptions
TypeDescription
DatastoreEmulatorException

if the clear fails

getProjectDirectory()

public synchronized File getProjectDirectory()
Returns
TypeDescription
File

start()

public synchronized void start()
Exceptions
TypeDescription
DatastoreEmulatorException

start(String emulatorDir, String projectId, String[] commandLineOptions) (deprecated)

public synchronized void start(String emulatorDir, String projectId, String[] commandLineOptions)

Deprecated. prefer setting options in the emulator options and calling {#start()}.

Starts the emulator. It is the caller's responsibility to call #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.

Parameters
NameDescription
emulatorDirString

The path to the emulator directory, e.g. /usr/local/cloud-datastore-emulator

projectIdString

The project ID

commandLineOptionsString[]

Command line options to pass to the emulator on startup

Exceptions
TypeDescription
DatastoreEmulatorException

If #start has already been called or the server does not start successfully.

stop()

public synchronized void stop()

Stops the emulator. Multiple calls are allowed.

Exceptions
TypeDescription
DatastoreEmulatorException

if the emulator cannot be stopped

stopEmulatorInternal()

protected void stopEmulatorInternal()
Exceptions
TypeDescription
DatastoreEmulatorException