grails - 由于 grails 2.4.4 中的 IllegalStateException 而无法运行集成测试

标签 grails intellij-idea integration-testing spock

我的应用程序使用 Grails 2.4.4。我为我的 Controller 编写了一个集成测试。

class UserControllerIntegrationSpec extends IntegrationSpec {
    UserController controller = new UserController()

    void "test something"() {
        when:
        controller.request.method = 'POST'
        controller.create()

        then:
        controller.response.status == HttpStatus.OK.value()
    }
}

当我尝试使用 IntelliJ 运行测试时,出现异常:

java.lang.IllegalStateException: Could not find ApplicationContext, configure Grails correctly first
    at grails.util.Holders.getApplicationContext(Holders.java:97)
    at grails.test.spock.IntegrationSpec.$spock_initializeSharedFields(IntegrationSpec.groovy:41)

当我使用带有 test-app 命令的控制台运行集成测试时,没有问题。附:单元测试工作正常。

最佳答案

您无法像单元测试那样在 IDE 中单独运行集成测试。集成测试环境的运行量几乎与使用 run-app 运行时一样多,其中一个很大的区别是没有 Web 服务器。

因此,测试基础设施期望有一个 Spring 应用程序上下文(至少部分依赖注入(inject)适用于测试类)、GORM、插件等。因此,您可以从 IDE 运行集成测试,但它们必须只需运行 grails test-app 并为您捕获输出,就像在 IDE 中运行应用程序一样。

关于grails - 由于 grails 2.4.4 中的 IllegalStateException 而无法运行集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27356739/

相关文章:

grails - 从Geb测试访问Spring bean

grails - 如何更改 Grails 3 中的每个插件编解码器?

intellij-idea - 为 Intellij 设置的 JBOSS_HOME 变量在哪里

grails - 如何测试从HTTP session 检索信息的Grails服务

java - Spring Boot集成测试未在应用程序主方法内执行初始化步骤

json - Grails - 如何让域类将 JSON 转换为域属性

grails - 如何重构Groovy中作为方法调用参数的闭包中的重复项?

elasticsearch - 在IntelliJ上调试elasticsearch github项目

java - 为什么我的课不被接受

c# - EntityFrameworkCore SQLite 内存数据库表未创建