Maven 单元测试 FileNotFound 错误
我创建了一个 Maven 项目,其中涉及在完成时“写出”一个 CSV 文件。
我创建了一个测试类来生成“模拟/测试”报告文件,但是在运行单元测试时,使用“mvn package”或“mvn test”命令时,我收到此错误:
28 Jan 2012 11:17:51,499 main DEBUG main.executors.ReportsExecutor:111 - Finished processing documents in 0.0 hours
28 Jan 2012 11:17:51,516 main ERROR main.utilities.FileWriterObject:279 - writeToADelimitedFile: reports/reportResults_1_28_2012_11.17.515.csv (No such file or directory) APPEND VALUE: false
28 Jan 2012 11:17:51,517 main ERROR main.executors.ReportsExecutor:170 - java.io.FileNotFoundException: reports/reportResults_1_28_2012_11.17.515.csv (No such file or directory)
java.io.FileNotFoundException: reports/reportResults_1_28_2012_11.17.515.csv (No such file or directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:102)
at java.io.FileWriter.<init>(FileWriter.java:61)
at
请记住“APPEND VALUE” : false”输出只是一个调试语句,让我知道 FileWriter 构造函数中的“append”布尔参数为 false,以便使用相应的文件名“创建”一个新文件。
现在,在生产中,它运行得很好。只是我的单元测试不起作用。是否有一些我没有配置的“根测试”目录?
我对 Maven 还很陌生。非常感谢任何反馈!
I created a Maven project that involves "writing out" a CSV file on completion.
I created a test class to generate a "mock/test" report file, but when running my unit test, when using the "mvn package" or "mvn test" commands, I get this error:
28 Jan 2012 11:17:51,499 main DEBUG main.executors.ReportsExecutor:111 - Finished processing documents in 0.0 hours
28 Jan 2012 11:17:51,516 main ERROR main.utilities.FileWriterObject:279 - writeToADelimitedFile: reports/reportResults_1_28_2012_11.17.515.csv (No such file or directory) APPEND VALUE: false
28 Jan 2012 11:17:51,517 main ERROR main.executors.ReportsExecutor:170 - java.io.FileNotFoundException: reports/reportResults_1_28_2012_11.17.515.csv (No such file or directory)
java.io.FileNotFoundException: reports/reportResults_1_28_2012_11.17.515.csv (No such file or directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:102)
at java.io.FileWriter.<init>(FileWriter.java:61)
at
Keep in mind that the "APPEND VALUE: false" output is just a debug statement letting me know that the "append" boolean parameter in the FileWriter constructor is false, in order to "create" a new file with the respective file name.
Now, in production, it works just fine. It's just my unit tests that aren't working. Is there some "root test" directory that I'm not configuring?
I'm quite green to Maven. Any feedback is greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Maven 设置了一个名为“basedir”的属性,可以方便地在单元测试中使用。我编写了一个快速单元测试,这在 Maven 和 Eclipse 中都适用:
当使用 System.getProperty(...) 获取“basedir”系统属性时,请务必将默认参数设置为“。”这样您也可以在 Eclipse 中运行单元测试:
Maven sets a property called "basedir" which is convenient to use in unit tests. I wrote a quick unit test and this works for me in both maven and in Eclipse:
When using System.getProperty(...) to get the "basedir" system property be sure to set the default parameter to "." so that you can run the unit test in Eclipse as well:
看起来我无法告诉 Maven “相对”目录运行其测试,除了我运行命令“mvn test”的目录之外。
因此,如果我尝试写入与项目根目录“相对”的目录中的文件,我需要让我的 Java 类(即进行写入的类)在写入之前检查以确保相应的目录存在。
我最初没有这样做的原因是因为我让 ant-deployment.xml 文件构建了我的实现所需的目录,即目标/日志、目标/资源等。所以当我运行我部署的应用程序时,我没有这个问题。
我希望我可以告诉maven,即“mvn -Dbase.dir=target test”,但显然我不能。
It looks like I can't tell Maven to run its tests "relative" to a directory, other than the one from where I ran the command "mvn test."
So if I'm trying to write to a file in a directory "relative" to the project root directory, I need to have my Java class, i.e. the one doing the writing, check to make sure the respective directory exists before writing it.
The reason why I didn't do this originally is because I have my ant-deployment.xml file build out the needed directories I need for my implementation, i.e. target/logs, target/resources, etc. So when I run my deployed app, I don't have this issue.
I was hoping that I could tell maven, i.e. "mvn -Dbase.dir=target test", but apparently I cannot.