# Class DatastoreEmulator (3.1.0)

3.1.0 (latest) 2.40.0 2.37.0 2.36.0 2.34.0 2.33.3 2.32.3 2.31.4 2.30.0 2.29.1 2.28.2 2.27.1 2.26.4 2.25.2 2.24.3 2.23.0 2.22.0 2.21.3 2.20.2 2.19.2 2.18.5 2.17.6

    public class DatastoreEmulator extends Datastore

An extension to [Datastore](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.datastore.v1.client.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](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> [Datastore](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.datastore.v1.client.Datastore) \> DatastoreEmulator

## Inherited Members

[Datastore.allocateIds(AllocateIdsRequest)](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.datastore.v1.client.Datastore#com_google_datastore_v1_client_Datastore_allocateIds_com_google_datastore_v1_AllocateIdsRequest_) [Datastore.beginTransaction(BeginTransactionRequest)](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.datastore.v1.client.Datastore#com_google_datastore_v1_client_Datastore_beginTransaction_com_google_datastore_v1_BeginTransactionRequest_) [Datastore.commit(CommitRequest)](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.datastore.v1.client.Datastore#com_google_datastore_v1_client_Datastore_commit_com_google_datastore_v1_CommitRequest_) [Datastore.getRpcCount()](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.datastore.v1.client.Datastore#com_google_datastore_v1_client_Datastore_getRpcCount__) [Datastore.lookup(LookupRequest)](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.datastore.v1.client.Datastore#com_google_datastore_v1_client_Datastore_lookup_com_google_datastore_v1_LookupRequest_) [Datastore.reserveIds(ReserveIdsRequest)](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.datastore.v1.client.Datastore#com_google_datastore_v1_client_Datastore_reserveIds_com_google_datastore_v1_ReserveIdsRequest_) [Datastore.resetRpcCount()](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.datastore.v1.client.Datastore#com_google_datastore_v1_client_Datastore_resetRpcCount__) [Datastore.rollback(RollbackRequest)](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.datastore.v1.client.Datastore#com_google_datastore_v1_client_Datastore_rollback_com_google_datastore_v1_RollbackRequest_) [Datastore.runAggregationQuery(RunAggregationQueryRequest)](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.datastore.v1.client.Datastore#com_google_datastore_v1_client_Datastore_runAggregationQuery_com_google_datastore_v1_RunAggregationQueryRequest_) [Datastore.runQuery(RunQueryRequest)](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.datastore.v1.client.Datastore#com_google_datastore_v1_client_Datastore_runQuery_com_google_datastore_v1_RunQueryRequest_) [Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) [Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) [Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) [Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) [Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) [Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) [Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) [Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) [Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) [Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) [Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)

## Methods

### clear()

    public void clear()

Clears all data in the emulator.

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.datastore.v1.client.DatastoreEmulatorException` | if the clear fails |

### getProjectDirectory()

    public synchronized File getProjectDirectory()

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/io/File.html` |   |

### start()

    public synchronized void start()

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.datastore.v1.client.DatastoreEmulatorException` |   |

### stop()

    public synchronized void stop()

Stops the emulator. Multiple calls are allowed.

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.datastore.v1.client.DatastoreEmulatorException` | if the emulator cannot be stopped |

### stopEmulatorInternal()

    protected void stopEmulatorInternal()

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.datastore.v1.client.DatastoreEmulatorException` |   |