title: Mocking a File using Mockito and PowerMockito
body: <p>I am writing a <code>JUnit</code> test case for a method that uses <code>FileWriter</code> to create a file but the file physically doesn't exist. I am using <code>PowerMockito</code> to mock the <code>File</code> Object without creating a real file. My test passes but it still creates a file by the name of <code>Mock for file,hashCode</code> in the context root of the project. Is there a way to avoid creating this file? There are some similar questions but not very clear why this is happening.So, while debugging I found out that when the code reaches to <code>new FileWriter</code> the file is created.</p>
  
  <p>Thanks,</p>
  
  <p>JUnit test:</p>
  
  <pre><code>@Test
  public void testCreateCSVFile() throws Exception {
      File mockFile = Mockito.mock(File.class);
      PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(mockFile);
      rest of the test...
  }
  </code></pre>
  
  <p>Method under test:</p>
  
  <pre><code>public void createCSVFile(List&lt;IUiIntegrationDto&gt; intgList, String fileName) throws Exception { 
       FileWriter fileWriter = new FileWriter(fileName);
       rest of the code...
  }
  </code></pre>