Search code examples
javajunitjunit4ioexception

JUnit Rule TemporaryFolder arbitrarily throws an IOException


I am facing a weird problem here...

I have a JUnit implementing a few tests. This class looks like the following:

public class MyTest {

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    @Test
    public void myTest1() throws IOException {
        String destinationPath = folder.newFile("destination1.txt").getPath();
        // Do things
    }

    @Test
    public void myTest2() throws IOException {
        String destinationPath = folder.newFile("destination2.txt").getPath();
        // Do things
    }

    @Test
    public void myTest3() throws IOException {
        String destinationPath = folder.newFile("destination.txt").getPath();
        // Do things
    }
}

This test class used to work in my previous environment and still does in Continuum.

Yet, when launching from Eclipse none, some, or all of the tests arbitrarily throw an IOException such as:

java.io.IOException: The system cannot find the path specified
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(File.java:883)
    at org.junit.rules.TemporaryFolder.newFile(TemporaryFolder.java:53)
    at MyTest.myTest2(MyTest.java:50)

I have the exact same problem running JUnit 4.9 or JUnit 4.10 ...

How can I fix that so that it works properly?


Solution

  • You should try disabling your anti-virus protection.

    I was having same problem, and after disabling Kaspersky everything was working just fine.