JUnit 4 测试用例在调试时通过,但在运行时失败

发布于 2024-12-25 19:55:56 字数 4424 浏览 0 评论 0原文

我正在使用 Eclipse Helios Service Release 2、Java 6 和 JUnit 4。

我有一个测试用例,当我自行调试它时该测试用例通过,而当我自行运行它时则失败。失败来自

FileUtils.deleteDirectory(target);

行,并出现错误

ERROR [main] (MyRequestHandler.java:56) - 处理 MyRequestHandler 时发生意外错误 java.io.IOException:无法删除文件:\apps\Codes\MP2\LandingStrips\inprocess\12345\testTarDir\myfile.txt

在这两种情况下,调用 File.canWrite(); 在程序尝试删除它之前返回 true。我唯一知道的其他事情是调试期间的运行时参数是

[-agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:2914, -Dfile.encoding=Cp1252]

和期间run are

[-Dfile.encoding=Cp1252]

我不确定这意味着什么或者是否重要,但为了提供尽可能多的背景知识 这是。

编辑-完整堆栈跟踪:

错误 [main] (MyRequestHandler.java:56) - 处理 MyRequestHandler 时发生意外错误 java.io.IOException:无法删除文件:\apps\Codes\MP2\LandingStrips\inprocess\12345\testTarDir\myfile.txt 在 org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:1643) 在org.apache.commons.io.FileUtils.cleanDirectory(FileUtils.java:1268) 在org.apache.commons.io.FileUtils.deleteDirectory(FileUtils.java:1200) 在 org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:1634) 在org.apache.commons.io.FileUtils.cleanDirectory(FileUtils.java:1268) 在org.apache.commons.io.FileUtils.deleteDirectory(FileUtils.java:1200) 在 com.codes.requesthandler.MyRequestHandler.deleteDirectory(MyRequestHandler.java:74) 在 com.codes.requesthandler.MyRequestHandler.process(MyRequestHandler.java:50) 在 com.codes.requesthandler.MyRequestHandlerTest.processWithProperArguments(MyRequestHandlerTest.java:135) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(来源未知) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(来源未知) 在 java.lang.reflect.Method.invoke(来源未知) 在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 在org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 在 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 在 org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) 在 org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 在 org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 在 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 在 org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:236) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

I'm using Eclipse Helios Service Release 2, Java 6 and JUnit 4.

I've got a test case that passes when I debug it by itself and fails when I run it by itself. The failure is coming from the line

FileUtils.deleteDirectory(target);

with the error

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

In both cases the call File.canWrite(); is coming back true before the proram attempts to delete it. The only other things I know is that the runtime arguments during debug are

[-agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:2914, -Dfile.encoding=Cp1252]

and during run are

[-Dfile.encoding=Cp1252]

I'm not sure what that means or if it matters, but for the sake of providing as much background as possible there it is.

EDIT- Full stack trace:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2025-01-01 19:55:56

可能相关..我有一个 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 中打开。

Possibly related .. I had a JUnit test that I would run from the command line that created a new data file and compared its contents with an existing data file using the org.apache.commons.io.FileUtils.contentEquals() method from the Apache Commons IO library - which I'm sure wraps the Java IO classes. The unit test passes if the contents of the new file match the contents of the old file exactly.

I always run the unit test using Ant in a separate terminal window - and not via Eclipse.
Just by having the project open in Eclipse would cause the test to fail approximately 25% of the time. (I ran the test repeatedly in a loop 1000 times and it would fail on average 250 of those times.) I suspect that the Eclipse Java Builder or one of the Eclipse plugins I have installed sometimes interfered with JUnit's access to the newly created data file.

Trying it today, I couldn't get the test to fail. Something in my project's or Eclipse's setup may have changed. I don't know what.

So my suggestion is to attempt to run your unit test via Ant - outside of Eclipse and with the project not open in Eclipse.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文