title: JUnit tests fail when creating new Files
body: <p>We have several JUnit tests that rely on creating new files and reading them. However there are issues with the files not being created properly. But this fault comes and goes. </p>
  
  <p>This is the code:</p>
  
  <pre><code>@Test
  public void test_3() throws Exception {
  
      // Deletes files in tmp test dir
  
      File tempDir = new File(TEST_ROOT, "tmp.dir");
      if (tempDir.exists()) {
          for (File f : tempDir.listFiles()) {
              f.delete();
          }
      } else {
          tempDir.mkdir();
      }
  
      File file_1 = new File(tempDir, "file1");
      FileWriter out_1 = new FileWriter(file_1);
      out_1.append("# File 1");
      out_1.close();
  
      File file_2 = new File(tempDir, "file2");
      FileWriter out_2 = new FileWriter(file_2);
      out_2.append("# File 2");
      out_2.close();
  
      File file_3 = new File(tempDir, "fileXXX");
      FileWriter out_3 = new FileWriter(file_3);
      out_3.append("# File 3");
      out_3.close();
              ....
  </code></pre>
  
  <p>The fail is that the second file object, file_2, never gets created. Sometimes. Then when we try to write to it a <code>FileNotFoundException</code> is thrown</p>
  
  <ul>
  <li>If we run only this testcase, everything works fine.</li>
  <li>If we run this testfile with some ~40 testcases, it can both fail and work depending on the current lunar cycle. </li>
  <li>If we run the entire testsuite, consisting of some 10*40 testcases, it always fails.</li>
  </ul>
  
  <p>We have tried</p>
  
  <ul>
  <li>adding sleeps (5sec) after <code>new File</code>, nothing</li>
  <li>adding while loop until <code>file_2.exists()</code> is true but the loop never stopped</li>
  <li>catching SecurityException, IOException and even throwable when we do the <code>New File(..)</code>, but caught nothing.</li>
  </ul>
  
  <p>At one point we got all files to be created, but file_2 was created before file_1 and a test that checked creation time failed.</p>
  
  <p>We've also tried adding <code>file_1.createNewFile()</code> and it always returns true.</p>
  
  <p>So what is going on? How can we make tests that depend on actual files and always be sure they exist?</p>
  
  <p>This has been tested in both java 1.5 and 1.6, also in Windows 7 and Linux. The only difference that can be observed is that sometimes a similar testcase before fails, and sometimes file_1 isn't created instead</p>
  
  <p><strong>Update</strong></p>
  
  <p>We tried a new variation:</p>
  
  <pre><code>File file_2 = new File(tempDir, "file2");
  while (!file_2.canRead()) {
      Thread.sleep(500);
      try {
          file_2.createNewFile();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>This results in alot of Exceptions of the type:</p>
  
  <pre><code>java.io.IOException: Access is denied
   at java.io.WinNTFileSystem.createFileExclusively(Native Method)
   at java.io.File.createNewFile(File.java:883)
  </code></pre>
  
  <p>... but eventually it works, the file is created.</p>