title: Grails mocking method in service which uses rest plugin
body: <p>I'm very new to Grails but have been using Ruby on Rails for the past few months, I can't seem to get my head around how to correctly Mock some functionality within a Service I have so I can properly Unit Test it.</p>
  
  <p>I have a <code>RestService</code> which uses a <code>RestBuilder</code> plugin</p>
  
  <pre><code>import javax.persistence.Converter;
  import org.codehaus.groovy.grails.web.json.JSONArray
  import org.json.JSONObject
  
  import grails.converters.JSON
  import grails.plugins.rest.client.RestBuilder
  import grails.transaction.Transactional
  
  @Transactional
  class RestService {
  
      def retrieveFromRESTClient(url) {
          System.properties.putAll( ["http.proxyHost":"some.proxy.host.com", "http.proxyPort":"8080", "https.proxyHost":"some.proxy.host.com", "https.proxyPort":"8080"] )
  
          def restBuilder = new RestBuilder()
          def clientResponse = restBuilder.get(url)
  
          // For development purposes 
          print "retrieveFromRESTClient: " + clientResponse.json
  
          return clientResponse
      }
  }
  </code></pre>
  
  <p>I'm attempting to write a Unit Test for <code>retrieveFromRESTClient()</code> and my thoughts are I should be Mocking the restBuilder.get() plugin call so it doesn't go off and actually do a get request to a URL during the Test. I've attempted a few things already such as extracting the plugin functionality to it's own method:</p>
  
  <pre><code>def retrieveFromRESTClient(url) {
      System.properties.putAll( ["http.proxyHost":"some.proxy.host.com", "http.proxyPort":"8080", "https.proxyHost":"some.proxy.host.com", "https.proxyPort":"8080"] )
  
      def clientResponse = getResponse(url)
  
      // For development purposes 
      print "retrieveFromRESTClient: " + clientResponse.json
  
      return clientResponse
  }
  
  def getResponse(url) {
      def restBuilder = new RestBuilder()
      def resp = restBuilder.get(url)
      resp
  }
  </code></pre>
  
  <p>and in my <code>RestServiceSpec</code> attempting to mock <code>getResponse</code></p>
  
  <pre><code>import org.springframework.http.ResponseEntity;
  
  import grails.plugins.rest.client.RestBuilder;
  import grails.test.mixin.TestFor
  import groovy.mock.interceptor.MockFor
  import spock.lang.Specification
  
  @TestFor(RestService)
  class RestServiceSpec extends Specification {
  
      def cleanup() {
      }
  
      void "retrieveFromRESTClient's responses JSON can be accessed"() {      
          when:
          service.metaClass.getResponse { ResponseEntity&lt;String&gt; foo -&gt; return new ResponseEntity(OK) }
          def resp = service.retrieveFromRESTClient("http://mocking.so.it.doesnt.matter/")
  
          then:
          print resp.dump()
          assert resp.json == [:]
      }
  }
  </code></pre>
  
  <p>Although this test passes, when I look at resp.dump() in the test-reports I see it's still gone and made a request to 'mocking.so.it.doesnt.matter' and returned that object instead of the mocked ResponseEntity which I assumed it would return.</p>
  
  <p>test-report output:</p>
  
  <pre><code>retrieveFromRESTClient: [:]&lt;grails.plugins.rest.client.RestResponse@433b546f responseEntity=&lt;404 Not Found,&lt;HTML&gt;&lt;HEAD&gt;
  &lt;TITLE&gt;Network Error&lt;/TITLE&gt;
  &lt;style&gt;
  body { font-family: sans-serif, Arial, Helvetica, Courier; font-size: 13px; background: #eeeeff;color: #000044 }
  li, td{font-family: Arial, Helvetica, sans-serif; font-size: 12px}
  hr {color: #3333cc; width=600; text-align=center}
  {color: #ffff00}
  text {color: #226600}
  a{color: #116600}
  &lt;/style&gt;
  &lt;/HEAD&gt;
  &lt;BODY&gt;
  &lt;big&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/big&gt;&lt;BR&gt;
  &lt;blockquote&gt;
  &lt;TABLE border=0 cellPadding=1 width="80%"&gt;
  &lt;TR&gt;&lt;TD&gt;
  &lt;big&gt;Network Error (dns_unresolved_hostname)&lt;/big&gt;
  &lt;BR&gt;
  &lt;BR&gt;
  &lt;/TD&gt;&lt;/TR&gt;
  &lt;TR&gt;&lt;TD&gt;
  RYLCR-BC-20
  &lt;/TD&gt;&lt;/TR&gt;
  &lt;TR&gt;&lt;TD&gt;
  Your requested host "mocking.so.it.doesnt.matter" could not be resolved by DNS.
  &lt;/TD&gt;&lt;/TR&gt;
  &lt;TR&gt;&lt;TD&gt;
  
  &lt;/TD&gt;&lt;/TR&gt;
  &lt;TR&gt;&lt;TD&gt;
  &lt;BR&gt;
  
  &lt;/TD&gt;&lt;/TR&gt;
  &lt;/TABLE&gt;
  &lt;/blockquote&gt;
  &lt;/BODY&gt;&lt;/HTML&gt;
  ,{Cache-Control=[no-cache], Pragma=[no-cache], Content-Type=[text/html; charset=utf-8], Proxy-Connection=[close], Connection=[close], Content-Length=[784]}&gt; encoding=UTF-8 $json=[:] $xml=null $text=null&gt;
  </code></pre>
  
  <p>My end goal is to bypass the plugins <code>get</code> call and return a <code>ResponseEntity</code> object. I'm not really sure if I'm using the correct approach for this?</p>