BrianGardner
BrianGardner

Reputation: 61

Grails 2.3 integration test behaving badly - ServletContext must not null

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

Upvotes: 3

Views: 1148

Answers (2)

kaskelotti
kaskelotti

Reputation: 4823

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.

Upvotes: 0

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27245

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.

Upvotes: 2

Related Questions