Search code examples
databaserestgrailsgrails-orm

Grails 2.5.0 - reset database to use test data


I would like to use separate testing software to create at least a portion of the tests for REST APIs. The other software runs tests faster and is also more portable. In order to use the testing software, I need to reset the database to have certain test data. Is there an easy way to have a service that drops all domain objects and creates a new "bootstrap" of data?


Solution

  • I suggest you to work with Grails environments. Simply define an environment rest-api-test. Within that environment you tell Hibernate to create-drop your database whenever Grails starts.

    Then define your test data set in the Bootstrap.groovy

    if (Environment.current.name == 'rest-api-test') {
       // insert your test data
    }
    

    Then simply use grails run-app -Dgrails.env=rest-api-test and wait unit Grails started your web application. Then start your external test tool.