简体   繁体   English

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

[英]Setting the property “java.io.tmpdir” causes test failure

I created a class to generate temporary files. 我创建了一个类来生成临时文件。 After that I started to test the class and wrote 2 tests, see below. 之后,我开始测试该类并编写了2个测试,请参见下文。 I excluded my generator class to be sure that none of my code causes interference. 我排除了生成器类,以确保没有任何代码引起干扰。

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");
    }
}

If I run the tests one-by-one, both of the tests will run successfully. 如果我一个接一个地运行测试,则两个测试都将成功运行。 But in the case of running the whole test file (by eclipse) the 'testCreateTemporaryFileShouldThrowException' will run in the first place - successfully, and 'testCreateTemporaryFile' will run secondly - with a failure. 但是,如果运行整个测试文件(通过Eclipse),则“ testCreateTemporaryFileShouldThrowException”将首先运行-成功,而“ testCreateTemporaryFile”将第二次运行-失败。 The failure is caused by an IOException: 失败是由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)

Anyone got an idea what is wrong? 有人知道出什么事了吗? I set the system property "java.io.tmpdir" always before a test runs and this is the only change that the second test makes that needs to be reset. 我总是在测试运行之前设置系统属性“ java.io.tmpdir”,这是第二个测试需要重置的唯一更改。

Doing this way will work: 这样做可以工作:

        @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)));
        }

It seems that you need to define the directory where Java will create your temp file. 似乎您需要定义Java将在其中创建临时文件的目录。 So, you need to set the third parameter of the method createTempFile which is the directory. 因此,您需要设置方法createTempFile的第三个参数,即目录。

Not setting this parameter, Java gets lost, not finding the correct path. 不设置此参数,Java会丢失,找不到正确的路径。 It might happen because you are changing an important system property. 这可能是因为您要更改重要的系统属性。

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

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