title: How to mock a service in an integration test for a dynamically scaffolded Grails controller?
body: <p>I'm trying to mock a service in an integration test for a dynamically scaffolded controller. I get an error indicating that the controller property for the service is not accessible from the test.</p>
  
  <p>It seems that dynamically scaffolded controllers can't be tested with unit tests at all so I'm using integration tests. I want to mock the service to test error handling in my app.  Is this a bug in Grails 2.2.0 or am I just doing it wrong?</p>
  
  <p>The result for <code>grails test-app</code> is:</p>
  
  <pre><code>groovy.lang.MissingPropertyException: No such property: myService for class: MyController
  </code></pre>
  
  <p>Example:</p>
  
  <p>I have modified the <code>src/templates/scaffolding/Controller.groovy</code>:</p>
  
  <pre><code>class ${className}Controller {
    MyService myService
  
    def action() {
      render myService.serviceMethod()
    }
  }
  </code></pre>
  
  <p>Dynamically scaffolded <code>MyController.groovy</code>:</p>
  
  <pre><code>class MyController {
      static scaffold = MyDomainClass
  }
  </code></pre>
  
  <p>Integration test <code>MyControllerTests.groovy</code>:</p>
  
  <pre><code>class MyControllerTests extends GroovyTestCase {
    def myController
  
    @Before
    void setUp() {
      myController = new MyController()
    }
  
    void testMock() {
      myController.myService = [ serviceMethod : { return "foo" } ] as MyService
      controller.action()
    }
  }
  </code></pre>