Groovy and Grails Integration Testing Headache

Posted on 2009-04-20
Last Modified: 2013-11-13
I'm working with Grails 1.1 and working through the book "Beginning Groovy and Grails from Novice to Professional".

First off - the author indicates that an integration test class will be automatically generated after the command to create a new domain class: create-domain-class todo.  No such tests are created - the unit one gets created, not the integration test.

My real problem however, is when I create my own integration test class in the "test/integration" directory - and implement the test (see code).

Works fine UNTIL I attempt to add any additional properties to the "Todo.groovy" class other than "name".

So when it's simply;

class Todo {

The integration tests pass without issue.  When I add something like;

class Todo {
      String name

Tests still pass.  BUT when I change the class to this;

class Todo {
      String name
      String note

The integration tests will not pass and don't provide any additional information other than the assertion failed;

Testcase: testPersist took 0.625 sec
      Caused an ERROR
Expression: (5 == Todo.count())
java.lang.AssertionError: Expression: (5 == Todo.count())
      at TodoIntegrationTests.testPersist(TodoIntegrationTests.groovy:23)
      at _GrailsTest_groovy$_run_closure4.doCall(_GrailsTest_groovy:202)
      at _GrailsTest_groovy$_run_closure2.doCall(_GrailsTest_groovy:146)
      at _GrailsTest_groovy$_run_closure1_closure19.doCall(_GrailsTest_groovy:112)
      at _GrailsTest_groovy$_run_closure1.doCall(_GrailsTest_groovy:95)
      at TestApp$_run_closure1.doCall(TestApp.groovy:66)
      at gant.Gant$_dispatch_closure4.doCall(Gant.groovy:324)
      at gant.Gant$_dispatch_closure6.doCall(Gant.groovy:334)
      at gant.Gant$_dispatch_closure6.doCall(Gant.groovy)
      at gant.Gant.withBuildListeners(Gant.groovy:344)
      at gant.Gant.this$2$withBuildListeners(Gant.groovy)
      at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
      at gant.Gant.dispatch(Gant.groovy:334)
      at gant.Gant.this$2$dispatch(Gant.groovy)
      at gant.Gant.invokeMethod(Gant.groovy)
      at gant.Gant.processTargets(Gant.groovy:495)
      at gant.Gant.processTargets(Gant.groovy:480)

Any assistance would be greatly appreciated.  Thanks in advance
public class TodoIntegrationTests extends GroovyTestCase {

	void setUp() {




	void testPersist() {


		new Todo(name:"1", createdDate:new Date(), priority:"", status:"").save()

		new Todo(name:"2", createdDate:new Date(), priority:"", status:"").save()

		new Todo(name:"3", createdDate:new Date(), priority:"", status:"").save()

		new Todo(name:"4", createdDate:new Date(), priority:"", status:"").save()

		new Todo(name:"5", createdDate:new Date(), priority:"", status:"").save()


		assert 5 == Todo.count()





Open in new window

Question by:superflie26
    LVL 35

    Expert Comment

    Looks like it's failing as there aren't 5 of the Todo objects saved

    Do you have constraints on the Todo class that could be stopping it getting saved?
    LVL 10

    Accepted Solution

    To get sure if it are really 5, just print the count before the check

    println Todo.count()
    assert 5 == Todo.count()

    Open in new window

    LVL 35

    Assisted Solution

    See the tips section on this page (4th heading):

    Tip: Saving domain object can fail without any error

    Author Comment

    Thanks for your participation - but I believe I have solved the problem;

    Properties named in a class are required when saving, unless you declare it as "nullable" in the static constraints closure;

    static constraints = {

    The "note" property of my Todo class is what was causing my problem - because in the integration test I was not passing a value for that property into the Todo constructor AND did not declare it as nullable in the class.
    LVL 35

    Expert Comment

    Not a split on the points? :-(

    Author Closing Comment

    Participation counts for something :-)  Thanks for being willing to hop in an lend a helping hand.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now