我正在使用Eclipse Helios Service Release 2,Java 6和JUnit 4。
我有一个测试用例,当我自己调试它时会通过,而当我自己运行它时会失败。失败来自
行 FileUtils.deleteDirectory(target);
错误
ERROR [main] (MyRequestHandler.java:56) - An unexpected error occurred when processing MyRequestHandler
java.io.IOException: Unable to delete file: \apps\Codes\MP2\LandingStrips\inprocess\12345\testTarDir\myfile.txt
在两种情况下,调用File.canWrite();
在proram尝试删除之前都会返回true。我知道的唯一其他事情是调试期间的运行时参数是
[-agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:2914, -Dfile.encoding=Cp1252]
并且在运行期间
[-Dfile.encoding=Cp1252]
我不确定这意味着什么或者是否重要,但是为了尽可能多地提供背景。
编辑 - 完整堆栈跟踪:
ERROR [main] (MyRequestHandler.java:56) - An unexpected error occurred when processing MyRequestHandler
java.io.IOException: Unable to delete file: \apps\Codes\MP2\LandingStrips\inprocess\12345\testTarDir\myfile.txt
at org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:1643)
at org.apache.commons.io.FileUtils.cleanDirectory(FileUtils.java:1268)
at org.apache.commons.io.FileUtils.deleteDirectory(FileUtils.java:1200)
at org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:1634)
at org.apache.commons.io.FileUtils.cleanDirectory(FileUtils.java:1268)
at org.apache.commons.io.FileUtils.deleteDirectory(FileUtils.java:1200)
at com.codes.requesthandler.MyRequestHandler.deleteDirectory(MyRequestHandler.java:74)
at com.codes.requesthandler.MyRequestHandler.process(MyRequestHandler.java:50)
at com.codes.requesthandler.MyRequestHandlerTest.processWithProperArguments(MyRequestHandlerTest.java:135)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
答案 0 :(得分:0)
可能相关..我有一个JUnit测试,我将从创建新数据文件的命令行运行,并使用org.apache.commons.io.FileUtils.contentEquals()将其内容与现有数据文件进行比较来自Apache Commons IO库的方法 - 我确信它包含了Java IO类。如果新文件的内容与旧文件的内容完全匹配,则单元测试通过。
我总是在一个单独的终端窗口中使用Ant运行单元测试 - 而不是通过Eclipse。 只是让项目在Eclipse中打开会导致测试在大约25%的时间内失败。 (我在循环中反复运行测试1000次,平均250次失败。)我怀疑我安装的Eclipse Java Builder或其中一个Eclipse插件有时会干扰JUnit对新创建的数据文件的访问
今天尝试一下,我无法让测试失败。我的项目或Eclipse的设置中的某些内容可能已更改。我不知道是什么。
所以我的建议是尝试通过Ant运行你的单元测试 - 在Eclipse之外并且项目没有在Eclipse中打开。