title: Grails domain unit testing - mockFor()
body: <p>This is the domain class:</p>
  
  <pre><code>class Registration {
  
    String email
    String generatedKey
  
    def beforeInsert = {
        String newToken = GlobalHelper.getRandomString()
        generatedKey = newToken
    }
  }
  </code></pre>
  
  <p>and this is the relevant part of the unit test:</p>
  
  <pre><code>    def c = mockFor(GlobalHelper)
      c.demand.static.getRandomString {-&gt; return "nestoABC" }
      c.createMock()
      reg.beforeInsert()
  </code></pre>
  
  <p>When running the test, I get this error:</p>
  
  <hr>
  
  <p>No such property: GlobalHelper for class: RegistrationTests</p>
  
  <p>groovy.lang.MissingPropertyException: No such property: GlobalHelper for class: RegistrationTests
  at RegistrationTests.testConstraints(RegistrationTests.groovy:57)</p>
  
  <hr>
  
  <p>The GlobalHelper class is located in Groovy source folder, and the mentioned line 57 is the line with the mockFor() method.</p>
  
  <p>Grails Testing docs were not very helpfull regarding this issue...</p>
  
  <p>I know this would be easily solved using integration tests, but I think it should also work this way.</p>
  
  <p>Thanks in advance</p>