title: Grails doesn't allow GORM in setupSpec() of Spock, but setup() happens after where
body: <p>In a Spock unit test for my Grails application, I am trying to create some mock domain objects that will be used across feature methods in a Spock test class.</p>
  
  <p>Sounds like a job for the setupSpec() block, right? Well:</p>
  
  <pre><code>class MyServiceSpec extends Specification {
      def setupSpec() {
          def author = new Author([name: "Bob"])
          def author2 = new Author([name: "Sally"])
          def author3 = new Author([name: "Alice"])
          def book1 = new Book([name: "My Story", author: author])
          def book2 = new Book([name: "My Favorite Stories", author: author2])
          def book3 = new Book([name: "My Least Favorite Stories", author: author3])
      }
  
      def "test my feature method"() {
          setup:
              def variable = service.myMethod(author, book)
          expect:
              //some relevant expectation
          where:
              //I'm using combinations() so I can test every possible combination of authors and books with my feature method
              [author, book] &lt;&lt; [Author.list() + [null], Book.list() + [null]].combinations()
      }
  }
  </code></pre>
  
  <p>Grails 2.3.6 will throw an error at this:</p>
  
  <blockquote>
    <p>org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    at com.my.package.MyServiceSpec.setupSpec(MyServiceSpec.groovy:17)</p>
  </blockquote>
  
  <p>It seems like GORM isn't legal during the setupSpec() fixture.</p>
  
  <h1>Workarounds I've tried</h1>
  
  <h2>Using the setup() block</h2>
  
  <p>If I change it so that the objects are created during the setup() block, they do get created, but the "test my feature method" won't have any of the objects available for the list() query. This is because where: operations occur before setup, even a setup() method outside the particular feature method.</p>
  
  <h2>Using the mockDomain method instead of the Mock annotation</h2>
  
  <p>If I </p>
  
  <pre><code>import grails.test.mixin.domain.DomainClassUnitTestMixin
  </code></pre>
  
  <p>Then in setupSpec, I can</p>
  
  <pre><code>def setupSpec()
      mockDomain(Author, [[name: "Bob"], [name: "Sally"], [name: "Alice"]])
  </code></pre>
  
  <p>That's a bit inconvenient, because then creating Books is harder, as the Author objects aren't bound to variables.</p>
  
  <p>The real problem with this approach is that even if I brute-find the Authors using Author.get(1), the objects don't get found by finders in later feature methods. In short, they aren't persisted to the minimal hibernate instance provided by Grails unit tests.</p>
  
  <h2>Using Spock Annotations To Run the setup just before the feature method</h2>
  
  <p>Following <a href="http://blog.10ne.org/2013/03/09/setup-once-per-feature-with-spock-extensions/" rel="nofollow">a suggestion from a blog post</a>, I tried adding in an annotation that should let you specify, "Hey, before this particular feature method, run this other method."</p>
  
  <p>In BuildConfig.groovy:</p>
  
  <pre><code>    mavenRepo "http://dl.bintray.com/content/noamt/java-libraries/"
      //...
      compile 'org._10ne.spock:spock-extensions:1.0'
  </code></pre>
  
  <p>Then I change my original setupSpec() block to be called featureSetup() and annotate my feature method with</p>
  
  <pre><code>@BeforeFeatureSetupExtensionAnnotation('featureSetup')
  def "test my feature method"() {
  ....
  </code></pre>
  
  <p>If I do this, I get the same error as before, only once for every feature method:</p>
  
  <blockquote>
    <p>Failure:  test my feature method(com.my.package.MyServiceSpec)
    org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    com.my.package.MyServiceSpec.featureSetup(MyServiceSpec.groovy:17)</p>
  </blockquote>