title: Inject dependencies in Grails Spock Specification test
body: <p>I need to get the dependencies injected in my domain objects in my tests.</p>
  
  <p>This tests are placed in the test/integration directory and extends from <code>spock.lang.Specification</code>.</p>
  
  <p>How can I achieve this?</p>
  
  <p>Note: I've seen this post <a href="https://stackoverflow.com/questions/9811345/how-to-inject-spring-beans-into-spock-test">How to inject spring beans into spock test</a>, but it is not related with grails.</p>
  
  <p>Edit:</p>
  
  <p>The dependency I want to get injected is <code>springSecurityService</code> in my <code>SecUser</code> subclass called <code>Player</code>. The method that is failing is the <code>encodePassword()</code>, which is called in the <code>beforeInsert()</code>.</p>
  
  <p>I can mock this <code>encodePassword()</code> method in some tests, but when I want to test my controllers method <code>save()</code>, I can't mock the <code>Player</code> that is being created because it all happens inside the controllers method.</p>
  
  <p>After changing to extend <code>IntegrationSpec</code>, this is my test code:</p>
  
  <pre><code>package intertigre.test.domain
  import intertigre.domain.Fecha;
  import intertigre.test.util.DomainFactoryTestService
  import grails.plugin.spock.IntegrationSpec
  import grails.test.mixin.TestFor
  
      @TestFor(Fecha)
      class FechaSpec extends IntegrationSpec{
  
      DomainFactoryTestService domainFactoryTestService = new DomainFactoryTestService()
  
      def 'test'(){
          given:
              def fecha = new Fecha()
          when:
              fecha.save()
          then:
              Fecha.get(1) == fecha
      }
  
  }
  </code></pre>
  
  <p>I'm getting this exception when running <code>grails test-app :spock</code>:</p>
  
  <pre><code>java.lang.NullPointerException: Cannot get property 'mainContext' on null object
      at grails.plugin.spock.IntegrationSpec.$spock_initializeSharedFields(IntegrationSpec.groovy)
  </code></pre>
  
  <p>And this one when I run the test alone:</p>
  
  <pre><code>| Failure:  intertigre.test.domain.FechaSpec
  |  java.lang.NullPointerException: Cannot get property 'autowireCapableBeanFactory' on null object
      at grails.plugin.spock.IntegrationSpec.setupSpec(IntegrationSpec.groovy:47)
  | Failure:  intertigre.test.domain.FechaSpec
  |  java.lang.NullPointerException: Cannot invoke method isActive() on null object
      at grails.test.mixin.support.GrailsUnitTestMixin.shutdownApplicationContext(GrailsUnitTestMixin.groovy:232)
      at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176)
      at org.spockframework.runtime.extension.builtin.JUnitFixtureMethodsExtension$FixtureType$FixtureMethodInterceptor.intercept(JUnitFixtureMethodsExtension.java:145)
      at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:84)
      at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176)
  </code></pre>