“找不到ApplicationContext,请先正确配置Grails" [英] "Could not find ApplicationContext, configure Grails correctly first"

查看:17
本文介绍了“找不到ApplicationContext,请先正确配置Grails"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个插件项目,其中包含一些域类和服务等.我们有一个使用插件项目的应用程序项目.这是一种常见的模式.集成测试(命中 DB)无法在插件项目中运行,因为它没有应用程序上下文,所以我们在主应用程序项目中运行集成测试.

We have a plugin project with some domain classes and services etc. We have an application project which uses the plugin project. This is a common pattern. Integration test (which hit the DB) cant run in the plugin project, as it has no application context, so we run the integration tests in the main application project.

我们有一个非常简单的集成测试:

We have a very simple integration test:

/*@TestFor(Site)*/
class SiteIntegrationSpec extends IntegrationSpec {
    static transactional=false;
    def setup() {
    }
    def cleanup() {
    }
    void "test something"() {
        Site site

        when:
        site = Site.get(1L)

        then:
        site.name == "root"
    }
}

站点只是一个域对象,类似这样:

The site is just a domain object, similar to this:

class Site {
    String name    
    // some more fields here
}

注意:尝试使用 TestFor(Site) 也未注释 - 同样的错误.

NOTE: tried it with TestFor(Site) un-commented also - same error.

如果我查看数据库,那里有站点条目.

If I look in the DB, there are site entries there.

好的,刚刚找到另一个线索.SiteIntegrationSpec 测试用于工作.在我们添加第二个测试 ParamIntegrationSpec 之前,它起作用了.如果我们这样单独运行这些测试中的任何一个:

OK, just found another clue. SiteIntegrationSpec test used to work. It worked before we added a second test, ParamIntegrationSpec. If we run either of these tests on their own thusly:

test-app --stacktrace --verbose ParamIntegrationSpec

作品

test-app --stacktrace --verbose SiteIntegrationSpec

作品

但是如果我们同时运行它们:

but if we run both of them:

test-app --stacktrace --verbose *IntegrationSpec

SiteIntegrationSpec 测试总是失败,出现上述异常.

The SiteIntegrationSpec test always fails with the above exception.

有什么想法吗?

完整的堆栈跟踪:

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)

注2:

test-app --stacktrace --verbose -integration

在站点测试中给出相同的错误.

gives the same error on Site test.

推荐答案

感谢 user1690588 我发现了问题.Grails IntegrationSpec IllegalStateException 给了我线索:probem 不在失败的测试中,而是在测试中通过了!

Thanks to user1690588 I found the problem. Grails IntegrationSpec IllegalStateException gave me the clue: the probem was not in the test which failed, but in the test which passed!

基本上,ParamIntegrationSpec 测试具有:

Basically, the ParamIntegrationSpec test had:

@TestFor(ParamService)

这会终止任何后续测试.我不知道 TestFor 是做什么的,只在所有示例中看到过.

This kills any subsequent test. I have no idea what TestFor does, only seen it in all examples.

要修复,只需从工作测试中删除该行.

To fix, just removed that line from the working test.

这篇关于“找不到ApplicationContext,请先正确配置Grails"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆