title: grails domain-class validator troubles testing
body: <p>I have a validator inside a domain class and I have a problem testing the controller for Lagerort. </p>
  
  <pre><code>com.example.xyz.LagerortControllerSpec &gt; Test the update action performs an update on a valid domain instance FAILED
  java.lang.IllegalStateException: Either class [com.example.xyz.Lagertyp] is not a domain class or GORM has not been initialized correctly or has already been shutdown. Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.
  </code></pre>
  
  <p>If I omit the validator, everything tests fine, but that's not what I want. </p>
  
  <p>The domain class:</p>
  
  <pre><code> class Lagerort {
  
       String lagerort
       Lagertyp lagertyp
       String beschreibung
  
       static auditable = true
  
       static constraints = {
           lagerort(nullable: false, blank: false, unique: true)
           lagertyp(nullable: false, blank: false, validator: { val, obj -&gt;
               // Only ONE Lagerort may be "Schrott"
               if (Lagertyp.count() &gt; 0) {
                   def _LAGERTYPSTRING="Schrott"
                   Lagertyp lagertypschrott = Lagertyp.findByLagertyp(_LAGERTYPSTRING)
                   if (obj.lagertyp == lagertypschrott &amp;&amp; Lagerort.countByLagertyp(lagertypschrott)&gt;0) return ['lagerortschrottunique',_LAGERTYPSTRING]
               }
           })
           beschreibung(nullable: false, blank: false)
  
       }
  
       String toString(){lagerort}
   }
  </code></pre>
  
  <p>The testCompile part of the dependencies in build.gradle looks like this:</p>
  
  <pre><code>testCompile "org.grails:grails-plugin-testing"
  testCompile "org.grails.plugins:geb"
  testCompile "org.grails.plugins:hibernate5"
  testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
  testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
  </code></pre>
  
  <p>I've already tried creating a few objects of type Lagertyp in the setup part of the controller tests so that <code>Lagertyp.count() &gt; 0</code> would be true for the validator, but that didn't help either. </p>
  
  <p>The populateValidParams of the LagerortControllerSpec / test looks like this: </p>
  
  <pre><code>def populateValidParams(params) {
      assert params != null
      params["lagerort"] = 'Fa.Conrad'
      params["lagertyp"] = ["lagertyp": 'Fa.Conrad', "beschreibung": 'Motor befindet sich bei Fa.Conrad']
      params["beschreibung"] = 'in Reparatur bei Fa. Conrad'
  }
  </code></pre>
  
  <p>The LagerortController: <a href="https://pastebin.com/PpZ5zqMm" rel="nofollow noreferrer">https://pastebin.com/PpZ5zqMm</a></p>
  
  <p>The test for LagerortController: <a href="https://pastebin.com/pxZ6UeVK" rel="nofollow noreferrer">https://pastebin.com/pxZ6UeVK</a></p>
  
  <p>Any ideas? </p>