Я создал класс для генерации временных файлов. После этого я начал тестировать класс и написал 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) «testCreateTevenFileShouldThrowException» будет выполняться в первую очередь - успешно, а «testCreateTevenFile» - во вторую - с ошибкой.
Ошибка вызвана 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" всегда перед запуском теста, и это единственное изменение, которое делает второй тест, который необходимо сбросить.