Tom
Tom

Reputation: 45144

Grails bootstrap on integration tests

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

In my BootStrap.groovy file it's 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 doesn't 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, I'm using IntelliJ Idea as an IDE.

Upvotes: 6

Views: 7558

Answers (4)

Peter
Peter

Reputation: 29867

From the 2.0 documentation:

Per Environment Bootstrapping

Its often desirable to run code when your application starts up on a per-environment basis. To do so you can use the grails-app/conf/BootStrap.groovy file's support for per-environment execution:

def init = { ServletContext ctx ->
    environments {
        production {
            ctx.setAttribute("env", "prod")
        }
        development {
            ctx.setAttribute("env", "dev")
        }
    }
    ctx.setAttribute("foo", "bar")
}

Upvotes: 2

Einar
Einar

Reputation: 3317

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

Upvotes: 11

Ray Tayek
Ray Tayek

Reputation: 10003

this works for me on 1.3.4:

    def init = { servletContext ->
        println 'bootstrap'
        switch (GrailsUtil.environment) {
            case "test":
            println 'test'
            Person p=new Person(name:'made in bootstrap')
            assert p.save();
            break
            }
    }
    def destroy = {
    }
}

this integration test passes:

@Test
void testBootStrapDataGotLoaded() {
    assertNotNull Person.findByName('made in bootstrap')
}

Upvotes: 0

Aaron Saunders
Aaron Saunders

Reputation: 33345

in BootStrap.groovy you can try something like this

if (!grails.util.GrailsUtil.environment.contains('test')) {
    log.info "In test env"
    println "Test environment"
    println "Executing BootStrapTest"
    new BootStrapTest().init()
    println "Finished BootStrapTest"
} else {
    log.info "not in test env"
}

Upvotes: 0

Related Questions