Grails 引导集成测试

发布于 2024-09-19 04:54:14 字数 916 浏览 5 评论 0原文

我正在尝试将一些测试数据插入到我的数据库中,由一个名为 BootStrapTest 的类来完成这项工作。

在我的 BootStrap.groovy 文件中,它的调用方式如下

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

        }

但是,当我运行集成测试时,此代码不会执行。我读过集成测试应该引导,所以我很困惑。

我看到了一些侵入性的解决方案,例如修改TestApp.groovy脚本,但我想有一条通过conf的途径来实现这一点。另请阅读这个SO问题这个也是如此,但不太明白。

也许我误解了一些东西,我在 grails 测试方面遇到了很多麻烦。如果它能带来什么好处,我会使用 IntelliJ Idea 作为 IDE。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你是我的挚爱i 2024-09-26 04:54:14

所有引导代码都必须从 Init 闭包中调用。所以这个版本应该可以工作:

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
    }

}

或者,您可以有一个单独的引导文件来插入测试数据,而不是调用 BootStrapTest.init()。 grails-app/conf 文件夹中名为 *BootStrap.groovy 的任何类(例如 TestBootStrap.groovy)都在引导阶段运行。请参阅http://www.grails.org/Bootstrap+Classes

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

枕梦 2024-09-26 04:54:14

来自 2.0 文档

按环境引导

通常当您的应用程序在每个环境的基础上启动时运行代码是理想的。为此,您可以使用 grails-app/conf/BootStrap.groovy 文件对每个环境执行的支持:

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

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")
}
沫雨熙 2024-09-26 04:54:14

在 BootStrap.groovy 中你可以尝试这样的事情

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

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"
}
无畏 2024-09-26 04:54:14

这在 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 = {
    }
}

此集成测试通过:

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

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')
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文