Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JUnit Rule TemporaryFolder任意抛出IOException_Java_Junit_Junit4_Ioexception - Fatal编程技术网

Java JUnit Rule TemporaryFolder任意抛出IOException

Java JUnit Rule TemporaryFolder任意抛出IOException,java,junit,junit4,ioexception,Java,Junit,Junit4,Ioexception,我在这里面临一个奇怪的问题 我有一个JUnit实现了一些测试。该类如下所示: public class MyTest { @Rule public TemporaryFolder folder = new TemporaryFolder(); @Test public void myTest1() throws IOException { String destinationPath = folder.newFile("destination1

我在这里面临一个奇怪的问题

我有一个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
    }
}
这个测试类过去在我以前的环境中工作,现在仍然是连续的

然而,当从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)
我在运行JUnit4.9或JUnit4.10时遇到了完全相同的问题


我如何修复它,使其正常工作?

从外观上看,这可能是一个与windows相关的问题,而不是JUnit问题。不知何故,当您以“受限权限用户”身份登录时,可能会丢失创建文件夹/文件的权限

我想您可以尝试自己创建一个临时文件夹,就像JUnit一样:

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

如果上面的语句引发相同的错误,您应该调查您的windows用户权限,或者尝试在“完全权限”用户下运行测试。

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


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

在我的情况下,似乎有帮助的是显式创建根文件夹;在您的情况下,将
文件夹.create()
放入代码的某个位置


丑陋,我知道。

那应该行得通。您使用哪个
Runner
运行测试?它们是同时发生的吗?您可能需要进行更多调试,也就是说,也可以使用
文件夹
变量查看问题发生时它指向的位置。您可以说明您在测试中正在做什么吗?你在写文件吗?另外,您是否运行了windows索引?是的,该程序的目的是处理数据并编写一个文件作为输出。没有运行windows索引。