Monday, September 21, 2009

Note to self: Grails integration tests fail with static delcaration before constraints

Ran into this today and it was semi-tough to hunt down so I figured I'd post it since lord knows I'll forget this sometime in the near future.

If you have a class as follows:

class User {
  String firstName
  String lastName

  static constraints = {
  }
}

And an integration test like so:

import grails.test.*

class UserIntegrationTests extends GrailsUnitTestCase {
  void testSave() {
    def user = new User(firstName:'Matt', lastName:'Woodward')
    assertNotNull user.save()
  }
}

You may find Grails throwing this error when you run your integration tests:

No signature of method User.save() is applicable for argument types:() values:[]

Bit of a weird error but from what I can tell, the integration tests don't like the way the scaffolding formats that constraints block. I reformatted without the line break at the tests pass, and now even when I put things back the way they were, the tests pass.

Hope that helps someone else's head and the nearest wall if you're starting to dig deeper into Grails.

3 comments:

Matthew Woodward said...

When I first started writing this post I thought the static declaration was what was causing the issue, hence the title. Through further experimentation while I was working through this, it doesn't seem to matter that it's static, it just blows up with the default code that's spit out by the scaffolding. Sorry I didn't update the title to make that more clear.

graemerocher said...

If its a unit test (inside test/unit) it will fail with this error, if its an integration test (inside test/integration) it should not. Unit tests require you to use the mocking API (like mockDomain)

Matthew Woodward said...

Thanks Graeme--could have sworn it was my integration test that was failing yesterday, but I did a quick test this morning and the integration test does work OK. Not sure what state things were in with my other app that led me down this path.