title: Java: How do I unit test a method that creates and manipulates a file within the scope of the method?
body: <p>I'm using JUnit and JMockit to create a unit test method that tests the creation and manipulation of a CSV file. How do I intercept the file that is being created to ensure that is created correctly?</p>
  
  <pre><code>@Test
  public void testPersistResultsAsCSV() throws IOException {
      final Long id = new Long(1234l);
  
      TableRow row = new TableRow();
      row.setValue(317l);
      row.setId(1234l);
  
      final List&lt;TableRow&gt; rows = new ArrayList&lt;TableRow&gt;();
      rows.add(row);
  
      final List&lt;TableRow&gt; empty = new ArrayList&lt;TableRow&gt;();
  
      new Expectations() {
          {
              tableService.findTableRowsById(id, (TableQueryOptions)any);
              result = rows;
  
              tableService.findTableRowsById(id, (TableQueryOptions)any);
              result = empty;
  
              fileDataService.saveResultsFile((ResultsFile) any);
  
          }
      };
  
      resultsToCsvService.persistResultsAsCSV(id);
  
  }
  </code></pre>