Note that there are some explanatory texts on larger screens.

plurals
  1. POGrails testing: New instance resolved to null if followed by .validate()
    primarykey
    data
    text
    <p>I've got this domain class <code>Ward</code>. It's got this constraint <code>name</code> that I'd like to test (<code>medi_abs</code> is the package <code>Ward</code> is located in):</p> <pre><code>package medi_abs class Ward { String name static constraints = { name blank:false } } </code></pre> <p>For now, the WardTests.groovy looks like the following:</p> <pre><code>package medi_abs @TestFor(Ward) @Mock(Ward) class WardTests { void testInstance() { def wardx = new Ward(name: 'OP') assertEquals 'OP', wardx.name } void testConstraints() { mockForConstraintsTests(Ward) def wardx = new Ward(name: 'OP') assertTrue wardx.validate() } } </code></pre> <p>When I run the <code>test-app Ward</code> command, testInstance will not fail, but testConstraints will, followed by this error message:</p> <pre><code>groovy.lang.MissingPropertyException: No such property: validate for class: medi_abs.WardTests at medi_abs.WardTests.testConstraints </code></pre> <p>[ EDIT: Now the error message I get is this, can't tell why it changed: ]</p> <pre><code>java.lang.NullPointerException at grails.test.MockUtils$_addValidateMethod_closure87.doCall(MockUtils.groovy:1022) at grails.test.MockUtils$_addValidateMethod_closure88.doCall(MockUtils.groovy:1065) at medi_abs.WardTests.testConstraints(WardTests.groovy:43) </code></pre> <p>It seems that in the first method, <code>Ward</code> is instantiated properly, but in connection with the validate() command, <code>wardx</code> is null. I could not find any good examples so far, am I using validate() wrong? Or is there even a better way to test constraints in Grails?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload