title: How can I mock a grails 4 service in a controller unit test
body: <p>I don't understand how mocking works in Grails 4.0.
  I have this unit test that fails (it is just an illustration of the real test, the real test actually involves the controller):</p>
  
  <pre><code>import com.myapp.MySearchService
  
  class ApiControllerSpec extends Specification implements ControllerUnitTest&lt;ApiController&gt; {
  
      def setup() {
      }
  
      def cleanup() {
      }
  
      void "test listSources"() {
          given:
          def mock = Mock(MySearchService) {
              find(_) &gt;&gt; [["label": "abc", "description": "xsad"]]
          }
  
          when:
          System.out.println(mock.find(''))
  
          then:
          1 * mock.find(_) &gt;&gt; [["label": "abc", "description": "xsad"]]
      }
  
  </code></pre>
  
  <p>with error</p>
  
  <pre><code>Too few invocations for:
  
  1 * mock.find(_) &gt;&gt; [["label": "abc", "description": "xsad"]]   (0 invocations)
  
  Unmatched invocations (ordered by similarity):
  
  1 * mock.invokeMethod('find', [&lt;java.lang.String@0 value= hash=0&gt;])
  
  
      at org.spockframework.mock.runtime.InteractionScope.verifyInteractions(InteractionScope.java:93)
      at org.spockframework.mock.runtime.MockController.leaveScope(MockController.java:77)
      at toe.ApiControllerSpec.test listSources(ApiControllerSpec.groovy:29)
  </code></pre>
  
  <p>and stdout</p>
  
  <pre><code>null
  </code></pre>
  
  <p>The interesting thing is that <em>this works fine if I use a very simple test class instead of MySearchService</em>. So I am assuming that it has to do something with the way Grails/Spring is setup. This might also explain the lines:</p>
  
  <pre><code>Unmatched invocations (ordered by similarity):
  
  1 * mock.invokeMethod('find', [&lt;java.lang.String@0 value= hash=0&gt;])
  </code></pre>
  
  <p>But how do I set it up otherwise? I can't find this anywhere in the documentation. Thanks for any help!</p>
  
  <p>Similar question (without answer):
  <a href="https://stackoverflow.com/questions/32104348/how-to-partially-mock-service-in-grails-integration-test">How to partially mock service in Grails integration test</a></p>