繁体   English   中英

JUnit Rule TemporaryFolder任意抛出IOException

[英]JUnit Rule TemporaryFolder arbitrarily throws an 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 ...

我该如何解决它以使其正常工作?

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

我遇到了同样的问题,在禁用卡巴斯基后,一切都运行得很好。

从它的外观来看,这可能是一个与Windows相关的问题而不是JUnit问题。 不知何故,您可能会在以“有限权限用户”身份登录时失去创建文件夹/文件的权利。

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

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

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

在我的案例中似乎有用的是显式创建根文件夹; 在你的情况下,在你的代码中的某处扔一个folder.create()

丑陋,我知道。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM