title: Grails Spock testing Controller and service
body: <p>Hi I have a controller called ApiController which uses a service called ApiService like so:</p>
  
  <pre><code>def createCategory(){
          def jsonObj = request.JSON
          jsonObj.each{ key, value -&gt;
              params.put(key,value)
          }
          render apiService.createCategory(params)
  }
  </code></pre>
  
  <p>Which works fine. 
  But I cannot seem to write a test for it.</p>
  
  <p>This is how far I have got:</p>
  
  <pre><code>@TestFor(ApiController)
  @Mock([Category,ApiService])   
  class CategorySpec extends Specification {
  
      def setup() {
      }
  
      def cleanup() {
      }
  
      void "test"() {
  
          setup:
          def apiService = Mock(ApiService)
  
          when:
          request.method = 'POST'
          request.requestMethod = 'POST'
          params.categoryID = 'test'
  
          controller.createCategory()
  
          then:
          println(response)
          1==1
  
      }
  </code></pre>
  
  <p>From this I get the following error:</p>
  
  <pre><code>java.lang.NullPointerException: Cannot invoke method createCategory() on null object
  </code></pre>
  
  <p>This is obviously because It cannot see my apiService bean. So My question is how do i do this in Spock?</p>