Grails 2.3 integration test behaves badly - ServletContext should not be null

I saw several posts about problems with Grails 2.3.x and integration testing, but nothing helped in my situation, so here it is:

I want to test my Grails services on a real 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 hope it will be something simple and dumb, but I'm afraid the problem will be solved by the Grails team.

Here's the code, properly cleaned up to remove any hint of where I'm working:

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, this is how you do it from Grails 2.3 onwards. I am consistently getting the following error:

java.lang.IllegalArgumentException: ServletContext must not be null

      

Any help is always appreciated.

Brian

+3


source to share


2 answers


One thing that can cause this problem is that yours is UserServiceSpec

defined in test/unit/

instead of test/integration

where it should be.



+2


source


I ran into this problem when I added a new integration test to my package. In this case, I expanded IntegrationSpec

on how it should be done with integration tests.



Unfortunately, other tests in terms of scope of integration have been performed incorrectly using annotations @Mock

and @TestFor

, which are only for unit tests . Correcting other tests removed the issue with the error message ServletContext must not be null

appearing with the new test.

0


source







All Articles