title: Grails Fixtures / Build Test Data plugin not working within a Unit Test
body: <p>I am trying to use the <a href="http://grails.org/plugin/fixtures" rel="nofollow">Grails Fixtures Plugin</a> within a service unit test. I am using grails 2.2.</p>
  
  <p>I have the next code in my BuildConfig.groovy</p>
  
  <pre><code>plugins {
      compile ":fixtures:1.2"
      compile ":build-test-data:2.0.3"
      ...
      ..
      .
  }
  </code></pre>
  
  <p>Initially I tried </p>
  
  <pre><code>@TestFor(HotelService)
  @Mock([Place])
  class HotelServiceTests {
      def fixtureLoader 
  
      void testFixturesInUnitTests() {
          def fixture = fixtureLoader.build {
              hotel(Place, name: "Hotel Hilton")
          }
      }
  }
  </code></pre>
  
  <p>The above test was failing:</p>
  
  <pre><code>|  java.lang.NullPointerException: Cannot invoke method build() on null object
  </code></pre>
  
  <p>With a Google Search I found in the <a href="http://grails.1312388.n4.nabble.com/Grails-user-f1312389.html" rel="nofollow">Grails User Mailing List</a> that the fixtureLoader have to createdexplicitly inside the unit test. </p>
  
  <pre><code>  import grails.plugin.fixtures.*
  
    @TestFor(HotelService)
      @Mock([Place])
      class HotelServiceTests {
          def fixtureLoader 
          def grailsApplication
  
          @Before 
          void setUp() {
              fixtureLoader = new FixtureLoader(grailsApplication)
          }
  
          void testFixturesInUnitTests() {
              def fixture = fixtureLoader.build {
                  hotel(Place, name: "Hotel Hilton")
              }
          }
      }
  </code></pre>
  
  <p>Test failed with the next error: </p>
  
  <pre><code> java.lang.NullPointerException: Cannot invoke method hasGrailsPlugin() on null object
      at grails.plugin.fixtures.builder.FixtureBuilder.lookForBuildTestDataPlugin(FixtureBuilder.groovy:53)
      at grails.plugin.fixtures.builder.FixtureBuilder.&lt;init&gt;(FixtureBuilder.groovy:49)
      at grails.plugin.fixtures.Fixture.createBuilder(Fixture.groovy:75)
      at grails.plugin.fixtures.Fixture.build(Fixture.groovy:44)
      at grails.plugin.fixtures.FixtureLoader.build(FixtureLoader.groovy:46)
  </code></pre>
  
  <p>Another GoogleSearch and I found a 'hack' to avoid the previous error.</p>
  
  <pre><code> import grails.plugin.fixtures.*
   import org.codehaus.groovy.grails.plugins.GrailsPluginManager
   import org.codehaus.groovy.grails.plugins.PluginManagerHolder
  
   @TestFor(HotelService)
      @Mock([Place])
      class HotelServiceTests {
          def fixtureLoader 
          def grailsApplication
  
          @Before 
          void setUp() {
              fixtureLoader = new FixtureLoader(grailsApplication)
              PluginManagerHolder.pluginManager = [hasGrailsPlugin: { String name -&gt; true }] as GrailsPluginManager 
          }
  
          void testFixturesInUnitTests() {
              def fixture = fixtureLoader.build {
                  hotel(Place, name: "Hotel Hilton")
              }
          }
      }
  </code></pre>
  
  <p>Now I am getting the next error: </p>
  
  <pre><code>java.lang.NullPointerException: Cannot invoke method isDomainClass() on null object
      at grails.plugin.fixtures.buildtestdata.BuildTestDataBeanDefinitionTranslator.translate(BuildTestDataBeanDefinitionTranslator.groovy:54)
      at grails.plugin.fixtures.buildtestdata.BuildTestDataBeanDefinitionTranslator.translate(BuildTestDataBeanDefinitionTranslator.groovy:43)
      at grails.plugin.fixtures.builder.FixtureBuilder.translateToBuild(FixtureBuilder.groovy:126)
      at grails.plugin.fixtures.builder.FixtureBuilder.invokeMethod(FixtureBuilder.groovy:121)
      at com.softamo.movilrural.TopRuralScraperServiceTests$_testFixturesInUnitTests_closure2.doCall(TopRuralScraperServiceTests.groovy:26)
  </code></pre>
  
  <p>I have no idea how to solve this. Any feedback is really welcome. </p>