5

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.

John Little
  • 10,707
  • 19
  • 86
  • 158
  • I have no idea why exactly this is happening but some other spec doing something which is polluting other specs. http://naleid.com/blog/2012/05/22/fixing-grails-tests-that-pass-in-isolation-but-fail-when-run-as-a-suite – MKB Jan 12 '15 at 11:29
  • Also try this: http://stackoverflow.com/questions/24957901/grails-integrationspec-illegalstateexception – MKB Jan 12 '15 at 11:29

1 Answers1

7

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.

Community
  • 1
  • 1
John Little
  • 10,707
  • 19
  • 86
  • 158
  • 3
    This is a good example of why copypasta is not an effective development strategy :) `@TestFor` is a _unit_ test annotation, and it mixes into your class fake behavior to get the unit test environment, where there's no real Spring/Hibernate/(most) plugins/etc. active, to look somewhat like the integration test and run-app environments. You shouldn't mix unit and integration test code (logically) but you can see that it also has negative effects on your tests. It could have been worse - it could have caused a false positive. – Burt Beckwith Jan 12 '15 at 12:34
  • 1
    TestFor for testing services brings that problem, but for testing controllers it injects the controller variable and instances it in an integration test. If that is not used, you need to create the controller instance by hand. So it is a little complicated and the Grails doc is not so good / complete on how to test controllers and services and unit vs integration. – Pablo Pazos Apr 15 '17 at 10:48