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.testP ersist(Tod oIntegrati onTests.gr oovy:23)
at _GrailsTest_groovy$_run_cl osure4.doC all(_Grail sTest_groo vy:202)
at _GrailsTest_groovy$_run_cl osure2.doC all(_Grail sTest_groo vy:146)
at _GrailsTest_groovy$_run_cl osure1_clo sure19.doC all(_Grail sTest_groo vy:112)
at _GrailsTest_groovy$_run_cl osure1.doC all(_Grail sTest_groo vy:95)
at TestApp$_run_closure1.doCa ll(TestApp .groovy:66 )
at gant.Gant$_dispatch_closur e4.doCall( Gant.groov y:324)
at gant.Gant$_dispatch_closur e6.doCall( Gant.groov y:334)
at gant.Gant$_dispatch_closur e6.doCall( Gant.groov y)
at gant.Gant.withBuildListene rs(Gant.gr oovy:344)
at gant.Gant.this$2$withBuild Listeners( Gant.groov y)
at gant.Gant$this$2$withBuild Listeners. callCurren t(Unknown Source)
at gant.Gant.dispatch(Gant.gr oovy:334)
at gant.Gant.this$2$dispatch( Gant.groov y)
at gant.Gant.invokeMethod(Gan t.groovy)
at gant.Gant.processTargets(G ant.groovy :495)
at gant.Gant.processTargets(G ant.groovy :480)
Any assistance would be greatly appreciated. Thanks in advance
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.testP
at _GrailsTest_groovy$_run_cl
at _GrailsTest_groovy$_run_cl
at _GrailsTest_groovy$_run_cl
at _GrailsTest_groovy$_run_cl
at TestApp$_run_closure1.doCa
at gant.Gant$_dispatch_closur
at gant.Gant$_dispatch_closur
at gant.Gant$_dispatch_closur
at gant.Gant.withBuildListene
at gant.Gant.this$2$withBuild
at gant.Gant$this$2$withBuild
at gant.Gant.dispatch(Gant.gr
at gant.Gant.this$2$dispatch(
at gant.Gant.invokeMethod(Gan
at gant.Gant.processTargets(G
at gant.Gant.processTargets(G
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()
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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? :-(
ASKER
Participation counts for something :-) Thanks for being willing to hop in an lend a helping hand.
Do you have constraints on the Todo class that could be stopping it getting saved?