“无法找到ApplicationContext,首先正确配置Grails”

时间:2015-01-12 11:03:13

标签: grails testing integration

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

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

/*@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"
    }
}

该网站只是一个域对象,类似于:

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

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

如果我查看数据库,那里有网站条目。

好的,刚发现另一条线索。 SiteIntegrationSpec测试用于工作。在我们添加第二个测试ParamIntegrationSpec之前,它工作正常。如果我们自己运行这些测试中的任何一个:

test-app --stacktrace --verbose ParamIntegrationSpec

作品

test-app --stacktrace --verbose SiteIntegrationSpec

作品

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

test-app --stacktrace --verbose *IntegrationSpec

SiteIntegrationSpec测试始终失败并出现上述异常。

任何想法?

完整堆栈跟踪:

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

在网站测试中给出相同的错误。

1 个答案:

答案 0 :(得分:6)

感谢user1690588,我发现了这个问题。 Grails IntegrationSpec IllegalStateException给了我线索:探测器没有在测试中失败,但在测试中通过了!

基本上,ParamIntegrationSpec测试有:

@TestFor(ParamService)

这会杀死任何后续测试。我不知道TestFor做了什么,只在所有例子中都看到过。

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