Create Default Data and Test Data the Easy Way Using Fixture and Build Test Data plugins

Posted By : Ankit Nigam | 30-Jun-2014
Hi Friends,
 
The most common approach we follow for creating some default data in our Web Applications is writing the code in "init closure in Bootstrap.groovy file" like for eg. we create some initial users using Bootstrap.groovy to login in our application.
 
The basic problems in this approach lies below :- 
  • If we are creating default data in Bootstrap, we are un-necessarily increasing the code there & it becomes un-manageable if we have more of that data.
  • If we are having different environments eg like in Development, Test or Production & different data for each,we might need to repeat the whole code in each block & increases the chances of getting Errors & violate DRY(do not repeat yourself) approach.
  • If we are creating a new User object there in Bootstrap, we need to fulfill all the contraints defined in the that User domain by providing all the necessary values , which might not be needed that time but even then we need to provide them as they are mandatory.
 
So for keeping a distance from the above discussed problems  we can make use of the two Plugins :
  • Build Test Data- It will add the flexibility to create the Domain Objects with providing only the value of required fields of the Domain like username, password for a user without providing any un-necessary field which is not required at that moment irrespective of the constraint provided by the Domain.The logic behind the stated approach is that "This Plugin Provide the Default values from its own if you do not Explicitly provide from your side", thus fullfilling all the contraints of the Domian,off the screen.
  • Fixture - It will generate a separate file in the fixture folder in your Application.This fixture file contains all the Objects creation Code using Build Test Data which you want to create when your Application is started.In Bootstrap you need to just tell the name of the fixture file which you need to create the data from.This can be separate for each Environment like for Delevelopment you have separate Fixture File, and different one for Test & Production also.

Add these lines to your BuildConfig.groovy to add thses plugins to you Application:-

compile ":fixtures:1.2"
compile ":build-test-data:2.0.9"
Using Fixture And Build Test Data Plugin:-
 
On installing the fixture plugin you will get a folder named fixture in Project Directory.This is the default location to find the fixture file by the application.A fixture file is nothing but a groovy file having a fixture code block in it.
Lets suppose we want to populate default user to login in our app using his username & password which are created using fixture & build test data.So what we do is to create a groovy file in fixture folder with a name eg testData.groovy & write in it :
fixture{
def user = User.build(username:"test",password:"test") // other attributes are populated using default values by Build Test Data plugin
}
Now the fixture file is ready now we need to load this fixture in our Bootstrap to populate this user in Database on Application startup.
def fixtureLoader   // inject fixture loader in order to user it
environments{
			development{
				fixtureLoader.load("Name_Of_Dev_Fixture_File")
			}
			production{
				fixtureLoader.load("Name_Of_Prod_Fixture_File")
			}
			test{
				fixtureLoader.load("Name_Of_Test_Fixture_File")
			}
		}
So this way you can create simple default or test data for your Domains & can load it into your application.This is a more benficial, manageable & organised approach.
If you have any questions regarding the above, feel free to comment.
 
Thanks,
Ankit

 

About Author

Author Image
Ankit Nigam

Ankit has worked on development of various SaaS applications using Grails technologies. He has good exposure on FFMPEG and video content management applications. Ankit likes mobile gaming and going out with friends.

Request for Proposal

Name is required

Comment is required

Sending message..