title: Mock a method for Grails Unit Test
body: <p>In one of my unit tests, I am having some difficulty getting a mocked method to executed. I have the following test code:</p>
  
  <pre><code>void testExample() {
      def mockICFService = new MockFor(ICFService)
      ...
  
      //Mock the methods
      controller.metaClass.icfList = { def person -&gt;
          println "icfList"
          return [new IC(conceptId:'12345')]
      }
      mockICFService.demand.getAllIC(1..1) { def id, def withHist, def appId -&gt;
          println "mocking service"
          return new Person()
      }
      ...
  
      def model = controller.detail()
  }
  </code></pre>
  
  <p>Inside of detail in my controller class I create a Person via the ICFService's getAllIC(). This part works correctly. Later in the function, however, there is a call to icfList (which is defined in the controller). Through println's I have determined that the call is still being made, although it is returning an empty array. I believe that this is because the array is populated based on data in the servletContext, but in Unit Testing there is no access to that (hence my trying to mock it out).</p>
  
  <p>Does anyone know how to force the test to use the mocked version of controller.icfList instead of calling the actual method in controller?</p>