title: How do you mock an output stream?
body: <p>By 'output steam' i mean any object which receives a sequence of bytes, or characters or whatever. So, java.io.OutputStream, but also java.io.Writer, javax.xml.stream.XMLStreamWriter's writeCharacters method, and so on.</p>
  
  <p>I'm writing mock-based tests for a class whose main function is to write a stream of data to one of these (the XMLStreamWriter, as it happens).</p>
  
  <p>The problem is that the stream of data is written in a series of calls to the write method, but what matters is not the calls, but the data. For example, given an XMLStreamWriter <code>out</code>, these:</p>
  
  <pre><code>out.writeCharacters("Hello, ");
  out.writeCharacters("world!");
  </code></pre>
  
  <p>Are equivalent to this:</p>
  
  <pre><code>out.writeCharacters("Hello, world!");
  </code></pre>
  
  <p>It really doesn't matter (for my purposes) which happens. There will be some particular sequence of calls, but i don't care what it is, so i don't want to write expectations for that particular sequence. I just want to expect a certain stream of data to be written any which way.</p>
  
  <p>One option would be to switch to state-based testing. I could accumulate the data in a buffer, and make assertions about it. But because i'm writing XML, that would mean making some fairly complex and ugly assertions. Mocking seems a much better way of dealing with the larger problem of writing XML.</p>
  
  <p>So how do i do this with a mock?</p>
  
  <p>I'm using <a href="http://code.google.com/p/moxiemocks/" rel="noreferrer">Moxie</a> for mocking, but i'm interested in hearing about approaches with any mocking library.</p>