title: How to mock FileInputStream and other *Streams
body: <p>I have class that gets GenericFile as input argument reads data and does some additional processing. I need to test it:</p>
  
  <pre><code>public class RealCardParser {
  
      public static final Logger l = LoggerFactory.getLogger(RealCardParser.class);
  
      @Handler
      public ArrayList&lt;String&gt; handle(GenericFile genericFile) throws IOException {
          ArrayList&lt;String&gt; strings = new ArrayList&lt;String&gt;();
          FileInputStream fstream = new FileInputStream((File) genericFile.getFile());
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br =  new BufferedReader(new InputStreamReader(in));
          String strLine = br.readLine();//skip header
          while ((strLine = br.readLine()) != null) {
              l.info("handling in parser: {}", strLine);
              strings.add(strLine);
          }
          br.close();
          return strings;
      }
  }
  </code></pre>
  
  <p>The issue is with new FileInputStream. I can mock GenericFile but it is useless cause FileInputStream checks if file exists. I changed my class so:</p>
  
  <pre><code>public class RealCardParser {
  
      public static final Logger l = LoggerFactory.getLogger(RealCardParser.class);
  
      protected BufferedReader getBufferedReader(GenericFile genericFile) throws FileNotFoundException {
          FileInputStream fstream = new FileInputStream((File) genericFile.getFile());
          DataInputStream in = new DataInputStream(fstream);
          return new BufferedReader(new InputStreamReader(in));
      }
  
      @Handler
      public ArrayList&lt;String&gt; handle(GenericFile genericFile) throws IOException {
          ArrayList&lt;String&gt; strings = new ArrayList&lt;String&gt;();
          BufferedReader br = getBufferedReader(genericFile);
          String strLine = br.readLine();//skip header
          while ((strLine = br.readLine()) != null) {
              l.info("handling in parser: {}", strLine);
              strings.add(strLine);
          }
          br.close();
          return strings;
      }
  }
  </code></pre>
  
  <p>So now I can override method getBufferedReader and test method handler:</p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class RealCardParserTest {
  
      RealCardParser parser;
  
      @Mock
      GenericFile genericFile;
  
      @Mock
      BufferedReader bufferedReader;
  
      @Mock
      File file;
  
      @Before
      public void setUp() throws Exception {
          parser = new RealCardParser() {
              @Override
              public BufferedReader getBufferedReader(GenericFile genericFile) throws FileNotFoundException {
                  return bufferedReader;
              }
          };
  
          when(genericFile.getFile()).thenReturn(file);
          when(bufferedReader.readLine()).thenReturn("header").thenReturn("1,2,3").thenReturn(null);
      }
  
      @Test
      public void testParser() throws Exception {
          parser.handle(genericFile);
          //do some asserts
      }
  }
  </code></pre>
  
  <p>Handler method now is covered with tests, but I still have uncovered method getBufferedReader that leads to cobertura problems.
  How to test method getBufferedReader or maybe there is another solution of the problem?</p>