[英]Setting the property “java.io.tmpdir” causes test failure
我創建了一個類來生成臨時文件。 之后,我開始測試該類並編寫了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”,這是第二個測試需要重置的唯一更改。
這樣做可以工作:
@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會丟失,找不到正確的路徑。 這可能是因為您要更改重要的系統屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.