2012-04-17 90 views
8

我在这里面临一个奇怪的问题......JUnit的规则TemporaryFolder随意抛出IOException

我实现了一些测试JUnit的。这个类看起来如下:

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 
    } 
} 

这个测试类用于工作在我以前的环境,仍然在Continuum中。

然而,从Eclipse的无启动时,一些或所有的测试随意抛出一个IOException如:

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) 

我有运行JUnit 4.9或JUnit的4.10完全相同的问题...

我该如何解决这个问题才能正常工作?

+0

这应该工作。你使用哪个'Runner'来运行测试?它们是并发的吗?您可能需要调试更多内容,即查看'folder'变量,以便在出现问题时查看它指向的位置。 – 2012-04-17 18:38:35

+0

你能说出你在测试中做什么吗?你正在写文件吗?此外,你有Windows索引运行? – 2012-05-30 06:45:43

+0

是的程序的目的是处理数据和写入文件作为输出。没有Windows索引正在运行。 – 2012-05-30 07:10:23

回答

1

您应该尝试禁用您的防病毒保护。

我遇到了同样的问题,禁用卡巴斯基后,一切正常。

0

从外观上看,这可能是一个与JUnit相关的更多的Windows相关问题。不知何故,您可能会错过在作为“有限权利用户”登录时创建文件夹/文件的权利。

我想你可以尝试创建一个临时文件夹yourslef,就像JUnit的作用:

 File folder= File.createTempFile("junit", ""); 

如果上面的语句抛出了同样的错误,你应该研究您的Windows用户权限,也许尝试运行下的测试一个“全权”用户。