GCP Datastore : Integration Testing using Spring Boot

In this article, I'll show you how you can create an in-memory datastore used by your tests.

Configuring a Datastore Profile Testing

Spring 3.1 introduced the bean definition profiles which can help us implementing an appropriate configuration for testing datastore staff. So, we'll have a specific profile for the GCP Datastore environment.

Basically, the GCPDatastoreTester() class helps us saving data to the local in-memory datastore.

Custom Annotation

In order to support the population of test data for each test case, we will create an annotation in which you can add the JSON file name as argument. In the test execution listener, we'll check for the existence of the annotation and load the data accordingly.

We'll use the annotation as follow :



Implementing Custom TestExecutionListener


The TestExecutionListener interface in the spring-test module defines a listener API for intercepting the events of the test case execution. 

Process :

  1. We check for the existence of the annotation 
  2. we will create an instance of LocalServiceTestHelper with LocalDatastoreServiceTestConfig which handles all the GCP environment setup before testing. 
  3. Load and insert data using the setDataset() method.




As you can see above we override only the beforeTestMethod() and afterTestMethod() in which we call also the setUp() and tearDown() methods.


How To Use

Because we use our own test execution listener, all default listeners are overridden. In order for the DI to work properly we've also added DependencyInjectionTestExecutionListener.

Comments

Popular posts from this blog

Spring JPA : Using Specification with Projection

Chip input using Reactive Form