Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Could not find ApplicationContext, configure Grails correctly first"

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.

like image 624
John Little Avatar asked Jan 12 '15 11:01

John Little


1 Answers

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.

like image 101
John Little Avatar answered Sep 21 '22 13:09

John Little