views:

31

answers:

1

Hi, 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.

Help please !

Thanks in advance.

+2  A: 

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)
Burt Beckwith
Its under IntegrationTests. What I'm trying to do is to test a controller, that retrieves the user object from session inside its save() method.
Tom
can you insert more code from your test?
Aaron Saunders
You shouldn't be getting a missing method exception unless you're doing something funky. Are you extending GroovyTestCase? Integration tests should, but unit tests would extend one of the Grails unit test classes like ControllerUnitTestCase.
Burt Beckwith