Not sure how to frame this question, but I am seeing some behavior that I am unable to explain ... any help will be highly appreciated. I have a super-class Form
, and sub-class DraftForm
. The super-class has a more stringent constraint on a property content(blank: false)
than the sub-class content(nullable: true)
, and I am using tablePerHierarchy false
. The domain classes are as follows.
class Form { String content static constraints = { content(blank: false) } static mapping = { tablePerHierarchy false } }
class DraftForm extends Form { static constraints = { content(nullable: true) } }
With the above domain models, the following test * passes * without any problem.
class DraftFormIntegrationSpec extends Specification { void "Testing a draft-form and a form derived from a draft-form"(){ given: "A draft-form with invalid form-fields" // 1 def draftForm = new DraftForm() when: "The draft-form is validated" // 2 assert draftForm.validate() == true then: "The draft-form has no error" // 3 !draftForm.hasErrors() when: "The draft-form is saved" // 4 try{ draftForm.save() }catch(Exception e){ println "Exception thrown!" println e.class } then: "The draft-form is not found in the database" // 5 draftForm.id == null when: "The draft-form is casted to a form" // 6 Form form = (Form) draftForm assert form.validate() == true then: "The form validates, and has no error" // 7 !form.hasErrors() } }
Here are my questions:
content
is null
(please see // 2 and // 3 in the test)?
DraftForm
is type casted to a Form
, it still validates fine and has no error (// 6 and // 7), even though the content
property is still null
. How is that possible?
Thank you for the help!
save()
return the instance or null so in your tests you need to useassert draftForm.save() != null
ordraftForm.save(failOnError:true)
. – user800014draftForm.save(failOnError: true)
without the try-catch, the test does fail by throwing an exception when it tries to save. However, I am puzzled that thedraftForm
validates without any issue (// 2) and has no error (// 3), but still I cannot save it. Although, the fact that I cannot save explainsdraftForm.id == null
(// 5), but still I cannot explain how it can validate without any error but still cannot save. – tikka