title: Mock File, FileReader and BufferedReader class using Mockito
body: <p>I got below code in a method which I want to test</p>
  
  <pre><code>File f = map.get("key1")
  BuffereReader r = new BufferedReader(new FileReader(f));
  String line=null;
  do {
      line=r.readLine();
  } while(r!=null);
  </code></pre>
  
  <p>I want to mock this operation so that I can pass the content of the file from the JUnit test case. I have done below:</p>
  
  <pre><code>Map fles = Mockito.mock(ConcurrentHashMap.class);
  File file = Mockito.mock(File.class);
  Mockito.when(files.get("key1")).thenReturn(file);
  
  FileReader fileReader = Mockito.mock(FileReader.class);
  BufferedReader bufferedReader = Mockito.mock(BufferedReader.class);
  
  try {
       PowerMockito.whenNew(FileReader.class).withArguments(file).thenReturn(fileReader);
       PowerMockito.whenNew(BufferedReader.class).withArguments(fileReader).thenReturn(bufferedReader);
       PowerMockito.when(bufferedReader.readLine()).thenReturn("line1")
           .thenReturn("line2").thenReturn("line3");
  
      } catch (Exception e) {
          Assert.fail();
      }
  </code></pre>
  
  <p>So basically I need to pass "line1", "line2" and "line3" as lines from the file which are read by the mocked BufferedReader.</p>
  
  <p>However upon doing that, it failes as NullPointerException when trying to instantiate new FileReader(f) part.</p>
  
  <p>So is it because I can't mock a BufferedReader or the approach is wrong?</p>
  
  <p>Thanks</p>