设置属性“ java.io.tmpdir”会导致测试失败

时间:2019-01-13 17:30:42

标签: java file testing ioexception temporary-files

我创建了一个用于生成临时文件的类。之后,我开始测试该类并编写了2个测试,请参见下文。我排除了生成器类,以确保我的代码都不会引起干扰。

public class TemporaryFileGeneratorTest {

    private static final String SYSTEM_TEMP_DIR_PROP = "java.io.tmpdir";

    private static final String DEFAULT_TEMP_DIR = System.getProperty(SYSTEM_TEMP_DIR_PROP);

    @Before
    public void setDefaultTempDir() {
        System.setProperty(SYSTEM_TEMP_DIR_PROP, DEFAULT_TEMP_DIR);
    }

    @Test
    public void testCreateTemporaryFile() throws IOException {
        File file = File.createTempFile("temp-file", ".txt");
        file.deleteOnExit();
    }

    @Test(expected = IOException.class)
    public void testCreateTemporaryFileShouldThrowException() throws IOException {
        System.setProperty(SYSTEM_TEMP_DIR_PROP, "not-existing");
        File file = File.createTempFile("cannot-create-file", ".txt");
    }
}

如果我一个接一个地运行测试,则两个测试都将成功运行。但是,如果运行整个测试文件(通过Eclipse),则“ testCreateTemporaryFileShouldThrowException”将首先运行-成功,而“ testCreateTemporaryFile”将第二次运行-失败。 该失败是由IOException引起的:

java.io.IOException: The system cannot find the path specified
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.createTempFile(File.java:2024)
    at java.io.File.createTempFile(File.java:2070)
    at mypackage.TemporaryFileGeneratorTest.testCreateTemporaryFile(TemporaryFileGeneratorTest.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

任何人都知道这是怎么回事?我总是在测试运行之前设置系统属性“ java.io.tmpdir”,这是第二个测试需要重置的唯一更改。

1 个答案:

答案 0 :(得分:0)

这样做会起作用:

        @Test
        public void testCreateTemporaryFile() throws IOException {
            File file = File.createTempFile("temp-file", ".txt", new File(System.getProperty(SYSTEM_TEMP_DIR_PROP)));
            file.deleteOnExit();
        }

        @Test(expected = IOException.class)
        public void testCreateTemporaryFileShouldThrowException() throws IOException {
            System.setProperty(SYSTEM_TEMP_DIR_PROP, "not-existing");
            File file = File.createTempFile("cannot-create-file", ".txt", new File(System.getProperty(SYSTEM_TEMP_DIR_PROP)));
        }

似乎您需要定义Java将在其中创建临时文件的目录。因此,您需要设置createTempFile方法的第三个参数,即目录。

不设置此参数,Java会丢失,找不到正确的路径。这可能是因为您要更改重要的系统属性。