title: Unit testing method that invokes FileWriter
body: <p>I'm trying to write a unit test for a method that calls a FileWriter. I am using JUnit 4.8, Powermock and Mockito. The method I'm testing looks like this. </p>
  
  <pre><code>    public void methodToTest(String fileName, String text){
  
            File file = new File (someStaticString) //string is a static field of the class and the path is a dir
  
            if(!file.exist()){ //create dir}
  
            try{
                 FileWriter fw = new FileWrite(file.getAbsolutePath() + "/" fileName, true);
                 fw.write(text);
                 fw.close();
            }
            catch(Exception e){
                 e.printStackTrace 
            }
      }
  </code></pre>
  
  <p>If file and fileWriter are declared outside the method, I can mock them using mockito and powermock so I can test the possible scenarios that may happen if the method was called since I'm practicing BDD. However, I cannot modify the codes I am testing since other developers created them (Though I think it's the developers responsibility to create unit test).</p>
  
  <p>I wondering how (if it's possible) to mock objects under a method like file and fw. How about if there are objects created under if() or loop()? Can they be mocked as well?</p>
  
  <p>If what I'm thinking is not possible, what recommendation can you suggest to test this kind of methods? I also learned about JUnit's TemporaryFolder. Can I use this in place of file to create Folders and file?</p>
  
  <p>Thanks. </p>