class Parent {
String name
String body
static constraints = {
name(blank: false, nullable:false)
body(blank: false, nullable:false)
}
}
class Child extends Parent{
static constraints = {
name(blank: true)
body(blank: true)
}
}
------------Tests using the testing plugin ---------------
import grails.test.GrailsUnitTestCase
import grails.test.MockUtils
class ChildTests extends GrailsUnitTestCase{
void testConstraints() {
// Mock the validate() method.
registerMetaClass(Child)
MockUtils.prepareForConstraintsTests(Child)
registerMetaClass(Parent)
MockUtils.prepareForConstraintsTests(Parent)
def testInstance = new Child()
def errors = testInstance.validate()
assertEquals 0, errors.size()
// Test the parent constraints
testInstance = new Parent(name:' ' )
errors = testInstance.validate()
assertEquals 2, errors.size()
assertEquals "blank", errors["name"]
assertEquals "nullable", errors["body"]
}
}
Happy testing :)
No comments:
Post a Comment