“Could not find ApplicationContext, configure Grails correctly first”

不打扰是莪最后的温柔 提交于 2020-01-21 07:43:07

问题


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
}

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

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

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

works

test-app --stacktrace --verbose SiteIntegrationSpec

works

but if we run both of them:

test-app --stacktrace --verbose *IntegrationSpec

The SiteIntegrationSpec test always fails with the above exception.

any ideas?

Full stack trace:

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)

Note2:

test-app --stacktrace --verbose -integration

gives the same error on Site test.


回答1:


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!

Basically, the ParamIntegrationSpec test had:

@TestFor(ParamService)

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.



来源:https://stackoverflow.com/questions/27900520/could-not-find-applicationcontext-configure-grails-correctly-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!