3

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

abdul
  • 1,531
  • 1
  • 14
  • 29
  • You have not provided enough information to say for sure what is wrong. See the sample app at https://github.com/jeffbrown/integrationtestdemo. The integration test at https://github.com/jeffbrown/integrationtestdemo/blob/master/test/integration/demo/UserServiceSpec.groovy passes. – Jeff Scott Brown Aug 15 '14 at 13:56
  • Jeff, perhaps it's a version difference, then. I'm using Groovy 2.1.6 with Grails 2.3.5. – BrianGardner Aug 15 '14 at 17:57
  • 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 Aug 15 '14 at 18:17
  • 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 Aug 15 '14 at 18:24
  • I'll upgrade and see what happens. – BrianGardner Aug 15 '14 at 19:05

2 Answers2

2

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.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • If you move the integration test in the app at https://github.com/jeffbrown/integrationtestdemo from `test/integration/` to `test/unit/`, you will see the `ServletContext must not be null` error. – Jeff Scott Brown Aug 15 '14 at 14:17
  • It seems to me that one could be certain this was the issue by running only integration tests `grails test-app integration:` and seeing if the test in question runs. – Ted Delezene Aug 15 '14 at 14:54
  • @TedDelezene It seems to me that one could be certain this was the issue by looking at the file system to see where the source file is defined and if it is under `test/unit/`, move it to `test/integration/` and run the test from there. – Jeff Scott Brown Aug 15 '14 at 14:56
  • There are other scenarios that lead to this same error message and there have been bugs in the past that have been addressed related to this. I am interested to know if the problem is just that the test is defined in the wrong directory. If there really is a bug that leads to this, I would like to fix it but in order to do that I need to know what kinds of scenarios trigger the problem. – Jeff Scott Brown Aug 15 '14 at 14:58
  • The test is in the proper location under "test/integration" and runs with "grails test-app integration:". All integration tests in that directory fail the same way. – BrianGardner Aug 15 '14 at 17:38
  • If you can share a sample app which demonstrates the problem, I expect there is a simple fix. There are any of a number of things that might be going wrong and it is impossible to know which one is relevant without seeing more code and knowing what your app is doing. – Jeff Scott Brown Aug 15 '14 at 17:47
0

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.

kaskelotti
  • 4,709
  • 9
  • 45
  • 72