Google Datastore unit testing using JUnit

pravanjan palai
2 min readNov 23, 2018

--

Google Datastore testing with JUnit framework

When you are working on a real-world application, writing unit tests for your code is a must. If you use Google datastore for your backend, then this article has step by step setup unit test for your datastore code.

The complete code is here on GitHub. If you have Git and Gradle install on your machine then do — git clone https://github.com/pravanjan/datastoretesting.git

This project has a simple example of setting up an environment and writing test cases of your data services classes.

This approach would help us to verify our Datastore query in realtime while writing code without starting up the localhost environment.

Let’s start the process by exploring the class under test.

This class initialize datastore services and saveTask save type with given key string.

This above class only has two methods saveTask takes two parameter -type and key string (unique id for each entity ) . and the second method takes only the key string(unique id ) and returns the entity. More detail on how Google Datastore unit testing API works.

Let’s write the test case for the above class. First, We need to set up the in-memory app engine Datastore.

LocalServiceTestHelper instance would be used to test the Datastore.

Then we write the test for TodoDataServices.

In the first test case, we verify the return key from the Datastore API save operation. This is done using the JUnit assert statement. The second test case verifies the getTask method and makes sure if we pass the correct key then it would query the entity with that key and get us the entity to verify the values.

--

--

No responses yet