Grails bootstrap on integration tests

Tom picture Tom · Sep 10, 2010 · Viewed 7.3k times · Source

Im trying to insert some test data into my database, for which a class called BootStrapTest does the work.

In my BootStrap.groovy file its called like this

environments {
            test {
                println "Test environment"
                println "Executing BootStrapTest"
                new BootStrapTest().init()
                println "Finished BootStrapTest"
            }

        }

However, when I run my integration tests, this code doesnt execute. I've read that integration tests should bootstrap, so i'm quite confused.

I saw some invasive solutions, such as modifying the TestApp.groovy script, but I would imagine that there is a road through conf to achieve this. Also read this SO question and this one as well, but didn't quite get it.

Maybe i'm misunderstanding something, I'm having a lot of trouble with grails testing. If it brings anything to the table, im using Intelli JIdea as an IDE.

Any thoughts will be appreciated.

Thanks in advance

Answer

Einar picture Einar · Nov 3, 2010

All bootstrap code must be called from the Init closure. So this version should work:

import grails.util.Environment

class BootStrap {  
    def init = { servletContext ->
        // init app
        if (Environment.current == Environment.TEST) {
            println "Test environment"
            println "Executing BootStrapTest"
            new BootStrapTest().init()
            println "Finished BootStrapTest"

        }
    }

    def destroy = {
        // destroy app
    }

}

Alternatively, you could have a seperate bootstrap file for inserting test data, rather than calling BootStrapTest.init(). Any class in the grails-app/conf folder which is named *BootStrap.groovy (e.g., TestBootStrap.groovy) is run in the bootstrap phase. See http://www.grails.org/Bootstrap+Classes