日志含义在windows环境下删除文件失败



我在windows环境下工作。

在我的tearDown()方法中,deleteSucceeded返回false。

文件永远不会被删除,尽管它在所需的位置。

奇怪的是:

在unix环境中,相同的代码可以正常工作。

参见下面的class:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/ServiceMockTest-context.xml" })
public class ServiceMockTest { 
private File actualSerializedObjectFile;
 @Before
 public void setUp() throws InvalidDocumentException, 
 SAXException, IOException, 
 ParserConfigurationException,  InvalidGpecConfigurationException {
 actualSerializedObjectFile = new File(OUTPUT_DIR, 
 "ServiceMockTest-" + name.getMethodName() + ".json");
 System.out.println("actualSerializedObjectFile: " 
 +  actualSerializedObjectFile.getAbsolutePath());
 }

 @After
 public void tearDown() {
     boolean deleteSucceeded = actualSerializedObjectFile.delete();
     if (!deleteSucceeded) {
         throw new RuntimeException("Failed to delete temporary 
         output file for List<Map<String,Object>> serialized 
         object: " + actualSerializedObjectFile.getAbsolutePath());
     }
 }
}

更新:

我分析了这个错误,我发现问题在别的地方。

当我在调试模式下运行测试时,控件转到以下代码块:

/**
 * Constructs a InvocationTargetException with a target exception.
 *
 * @param target the target exception
 */
public InvocationTargetException(Throwable target) {
    super((Throwable)null);  // Disallow initCause
    this.target = target;
}

这发生在调用delete()方法之后。

注意:

我的文件没有在其他任何地方打开,以便delete()调用不会删除它,因为在windows环境中可能是这种情况。

ErrorLog:

java.lang.RuntimeException: Failed to delete temporary output file for List<Map<String,Object>> serialized object: E:gituserappservicesserializedObjectsServiceMockTest-getSchedule.json
    at com.userapp.services.ServiceMockTest.tearDown(ServiceMockTest.java:198)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:36)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:47)
    at org.junit.rules.RunRules.evaluate(RunRules.java:18)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Windows不允许您删除应用程序仍打开的文件,但Unix可以。这可能就是导致行为差异的原因。

在windows环境中需要关闭inputStream以便在需要时删除文件。

我只是在调用delete之前添加了inputStream.close();,它工作了。

这也意味着在unix环境中,一旦读取流的工作完成,inputStream将自行关闭。

相关内容

  • 没有找到相关文章

最新更新