Search code examples
unit-testinggrailsintegration-testingmockinggrails-orm

Grails Integration testing: problems with get


I'm trying to write a simple integration test, but having some trouble with Domain Objects. I've read on unit testing but can't figure it out.

This is my simple test:

    User user = User.get(1)

    controller.params.userid = "1"
    controller.session.user = user

    controller.save();

The error message is:

groovy.lang.MissingMethodException: No signature of method: static com.baufest.insside.user.User.get() is applicable for argument types: (java.lang.Integer) values: 1

My guess is that I should mock the user object, but don't know how.


Solution

  • You say that you're integration testing, but it looks like you're unit testing. Is the test under test/integration or test/unit? Unit tests need mocking, but integration tests have an initialized Spring application context and Hibernate, and run against an in-memory database.

    This is described in the user guide, which is at http://grails.org/doc/latest/ (you reference an older 1.1 version).

    To mock the User class, just call mockDomain with one or more test instances either in setUp or in the test method:

    def users = [new User(...), new User(...), ...]
    mockDomain User, users
    
    ...
    
    User user = User.get(1)