Link to home
Start Free TrialLog in
Avatar of superflie26
superflie26

asked on

Groovy and Grails Integration Testing Headache

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() {
		Todo.list()*.delete()
	}
	
	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

Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

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?
ASKER CERTIFIED SOLUTION
Avatar of mahome
mahome
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of superflie26
superflie26

ASKER

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 = {
     note(nullable:true)
}

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.
Not a split on the points? :-(
Participation counts for something :-)  Thanks for being willing to hop in an lend a helping hand.