Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails bootstrap on integration tests

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

like image 342
Tom Avatar asked Sep 10 '10 15:09

Tom


1 Answers

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

like image 91
Einar Avatar answered Sep 21 '22 23:09

Einar