title: How to mock a HttpServletRequest's BufferedReader payload in Grails/Spock
body: <p>I have a Grails controller that expects an XML payload.</p>
  
  <p>I fetch the XML payload like this in a Grails controller.</p>
  
  <pre><code>def xmlPayload = request.reader.text
  </code></pre>
  
  <p>That part works fine, but I'm struggling to mock this payload in a unit test.</p>
  
  <p>I've tried both of the following, but the debugger is showing 'request.reader' to be null in both approaches.</p>
  
  <p>Approach #1:</p>
  
  <pre><code>void "test controller method"(){
      setup:
      def mockBufferedReader = Mock( BufferedReader )
      mockBufferedReader.getText() &gt;&gt; '&lt;hello/&gt;'
      request.getReader() &gt;&gt; mockBufferedReader
      ....
  </code></pre>
  
  <p>Approach #2:</p>
  
  <pre><code>void "test controller method"(){
      setup:
      def mockBufferedReader = Mock( BufferedReader )
      mockBufferedReader.getText() &gt;&gt; '&lt;hello/&gt;'
      request.metaClass.getReader = { -&gt; mockBufferedReader }
      ....
  </code></pre>
  
  <p>'request' in a unit test is a GrailsMockHttpServletRequest, so I presumed I could mock its methods like this (3rd line of both approaches), but so far no luck.</p>
  
  <p>Thanks for any ideas.</p>