{"id":643,"date":"2010-05-06T17:18:29","date_gmt":"2010-05-06T11:48:29","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=643"},"modified":"2016-12-19T15:28:30","modified_gmt":"2016-12-19T09:58:30","slug":"save-time-while-writing-integration-tests","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/save-time-while-writing-integration-tests\/","title":{"rendered":"Grails : Save time while writing Integration tests"},"content":{"rendered":"<p>Hi Friends,<\/p>\n<p>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&#8217;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.<\/p>\n<blockquote>\n<pre lang=\"sh\">\r\nmysqldump --user=myusername --password=mypassword databaseName  \/home\/amit\/proj_dev_backup.sql\r\nmysql -u myusername --password=mypassword < \/home\/amit\/updateTestDatabase.sql\r\n\r\ncd \/home\/amit\/applicationPath\r\nmv .\/grails-app\/conf\/BootStrap.groovy \/home\/amit\/BootStrap_org.groovy\r\ncp \/home\/amit\/BootStrap.groovy .\/grails-app\/conf\/\r\n\r\ngrails test-app -integration $@\r\n\r\nmv \/home\/amit\/BootStrap_org.groovy .\/grails-app\/conf\/BootStrap.groovy\r\nrm  \/home\/amit\/proj_dev_backup.sql\r\n <\/pre>\n<p>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.<\/p>\n<p>Following is the updateTestDatabase.sql file used in the above script which creates and updates the test database.<\/p>\n<blockquote>\n<pre lang=\"sql\">\r\n drop database testDatabaseName; create database testDatabaseName;\r\n use testDatabaseName;\r\n source \/home\/amit\/proj_dev_backup.sql\r\n exit\r\n<\/pre>\n<p>We also need to update DataSource.groovy file for the test environment as follows: <\/p>\n<pre lang=\"groovy\">\r\ntest {\r\n\tdataSource {\r\n             dbCreate = \"update\" \r\n             url = \"jdbc:mysql:\/\/localhost\/testDatabaseName\"\r\n\t     driverClassName = \"com.mysql.jdbc.Driver\"\r\n\t     username = \"myusername\"\r\n\t     password = \"mypassword\"\r\n       }\r\n}\r\n<\/pre>\n<p>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. <\/p>\n<p>Hope this helped!<\/p>\n<p>~~Amit Jain~~<br \/>\namit@intelligrape.com<br \/>\nhttp:\/\/www.tothenew.com\/blog\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;t we restore the database once created, before we [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[7],"tags":[4840,142,231],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/643"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=643"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/643\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}