3
votes

I've seen a few posts about issues with Grails 2.3.x and integration testing, but nothing has helped my situation yet, so here goes:

I want to test my Grails services against a real live database (Oracle) and so I wrote some integration tests in Spock. No matter which of the recommended approaches I try, I get the same error. I'm hoping it's something simple and dumb, but I fear that there is an issue which needs to be addressed by the Grails team.

Here's the code, properly sanitized to remove any hint of where I work:

package com.mycompany

import grails.test.spock.IntegrationSpec
import spock.lang.*
import com.mycompany.User

class UserServiceSpec extends IntegrationSpec {

    UserService userService

    def setup() {
    }

    def cleanup() {
    }

    void "find a user by their id"() {
            when:
                User user = userService.find('1234')
        then:
                user.firstName == 'Brian'
    }
}

From everything I've read out there, this is how you do it with Grails 2.3 and beyond. I consistently get the following error

java.lang.IllegalArgumentException: ServletContext must not be null

Any help is always appreciated.

Brian

2
You have not provided enough information to say for sure what is wrong. See the sample app at github.com/jeffbrown/integrationtestdemo. The integration test at github.com/jeffbrown/integrationtestdemo/blob/master/test/… passes. - Jeff Scott Brown
Jeff, perhaps it's a version difference, then. I'm using Groovy 2.1.6 with Grails 2.3.5. - BrianGardner
I don't think Groovy 2.1.6 is supported with Grails 2.3.5. We shipped 2.3.5 with Groovy 2.1.9. Did you build your own dist or was that just a typo? - Jeff Scott Brown
I just pushed a change to that repo which downgrades the app to 2.3.5 and the test still passes for me. Does it fail for you? - Jeff Scott Brown
I'll upgrade and see what happens. - BrianGardner

2 Answers

2
votes

One thing that can cause that problem is if your UserServiceSpec is defined under test/unit/ instead of test/integration where it is supposed to be.

0
votes

I came across this issue when I added a new integration test to my suite. I extended IntegrationSpec in this case as should be done with integration tests.

Unfortunately, the other tests on integration scope were done incorrectly by using @Mock and @TestFor annotations, which are meant for unit tests only. Fixing the other tests, removed the problem of ServletContext must not be null error message appearing with the new test.