开发者

Grails bootstrap on integration tests

开发者 https://www.devze.com 2023-01-15 09:41 出处:网络
Im trying to insert some test data into my database, for which a class called BootStrapTestdoes the work.

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


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


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")
}


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"
}


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')
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号