I am working on a grails application which have around 500 grails test cases (unit + integration). Recently I added some new test cases and those test cases working fine when running in single but failing when running with all other test cases. I was getting “Cannot find model on null object”.

class UserControllerSpec extends IntegrationSpec {

    UserController controller
    Map renderedMap

    public void setup() {
        controller = new UserController()
        controller.metaClass.render = { Map map ->
            renderedMap = map
        }
    }

    public void cleanup() {

    }

    void "test list"(){
        when:
        controller.list()

        then:
        renderedMap.model.userInstanceList.size() > 0
    }
}

I search for this and found that this is one of the test pollution problem. Test pollution can happens by several reasons. In my case it was happening because of metaClass changes which I have not removed.

In case of metaClass changes, this problem solved by removing these changes.

public void cleanup() {
    controller.metaClass = null
}

Hope this helps 🙂 .