Grails : Save time while writing Integration tests

Hi Friends,

I always used to spend lot of time writing integration tests as running even a single test involves loading the complete application, which includes loading complete database every time. Then I realized that data which I use for testing is always the same, why can’t we restore the database once created, before we run the test so that at least time spent on loading data can be saved. Following is the script which I wrote and worked for me.

mysqldump --user=myusername --password=mypassword databaseName  /home/amit/proj_dev_backup.sql
mysql -u myusername --password=mypassword < /home/amit/updateTestDatabase.sql
cd /home/amit/applicationPath
mv ./grails-app/conf/BootStrap.groovy /home/amit/BootStrap_org.groovy
cp /home/amit/BootStrap.groovy ./grails-app/conf/
grails test-app -integration $@
mv /home/amit/BootStrap_org.groovy ./grails-app/conf/BootStrap.groovy
rm  /home/amit/proj_dev_backup.sql

In the above script, databaseName is to be replaced with my development environment database, which is expected to be updated. Here BootStrap.groovy file gets replaced temporarily. If we like, we can also load data in a bootstrap.groovy only for development environment, then we won’t need to replace it even temporarily as in the above script.

Following is the updateTestDatabase.sql file used in the above script which creates and updates the test database.

 DROP DATABASE testDatabaseName; CREATE DATABASE testDatabaseName;
 USE testDatabaseName;
 source /home/amit/proj_dev_backup.sql

We also need to update DataSource.groovy file for the test environment as follows:

test {
	dataSource {
             dbCreate = "update" 
             url = "jdbc:mysql://localhost/testDatabaseName"
	     driverClassName = "com.mysql.jdbc.Driver"
	     username = "myusername"
	     password = "mypassword"

Now I just run this script, provides the list of files to be tested (if any) as command line arguments, and see the integration test results much faster then ever before.

Hope this helped!

~~Amit Jain~~

3 thoughts on “Grails : Save time while writing Integration tests

  1. Pingback: sql wildcard,sql wildcards,sql rollback,rollback sql,sql copy table,sql sum,sql mirroring,sum sql,sql cluster,sql server performance,truncate in sql,backup sql,backup sql database,backup sql server,sql performance,date functions in sql,sql over,truncate s

  2. Have you considered making an extra environment flag and using a segment in the bootstrap to do your custom stuff, and just leave it all in one bootstrap?

    amitEnv {
    if(enviroment==”amitEnv”) {
    // do stuff

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>