Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1265
  • Last Modified:

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

0
superflie26
Asked:
superflie26
  • 3
  • 2
2 Solutions
 
TimYatesCommented:
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?
0
 
mahomeCommented:
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

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

http://jan-so.blogspot.com/2008/12/grails-integration-testing-some-tips.html

----------------
Tip: Saving domain object can fail without any error
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now