JUnit 规则临时文件夹
我正在 JUnit 4.7 中使用 @Rule
注释创建一个 TemporaryFolder
。我尝试在我的 @Before
(设置)方法中使用 tempFolder.newFolder("someFolder")
创建一个新文件夹,该文件夹是临时文件夹的子文件夹测试。似乎临时文件夹在安装方法运行后被初始化,这意味着我无法在安装方法中使用临时文件夹。这是正确的(且可预测的)行为吗?
I'm creating a TemporaryFolder
using the @Rule
annotation in JUnit 4.7. I've tried to create a new folder that is a child of the temp folder using tempFolder.newFolder("someFolder")
in the @Before
(setup) method of my test. It seems as though the temporary folder gets initialized after the setup method runs, meaning I can't use the temporary folder in the setup method. Is this correct (and predictable) behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 Junit 4.7 中的问题。如果您升级较新的 Junit(例如 4.8.1),则当您输入 @Before 方法时,所有 @Rule 都将运行。相关的错误报告是这样的: https://github.com/junit-team/junit4 /问题/79
This is a problem in Junit 4.7. If you upgrade a newer Junit (for example 4.8.1) all @Rule will have been run when you enter the @Before method:s. A related bug report is this: https://github.com/junit-team/junit4/issues/79
这也有效。 编辑 如果在 @Before 方法中看起来需要调用 myfolder.create() 。这可能是不好的做法,因为 javadoc 说不要调用 TemporaryFolder.create()。 第二次编辑 看起来,如果您不想在 @Test 方法中使用它们,则必须调用方法来创建临时目录。另请确保关闭在临时目录中打开的所有文件,否则它们不会被自动删除。
This works as well. EDIT if in the @Before method it looks like myfolder.create() needs called. And this is probably bad practice since the javadoc says not to call TemporaryFolder.create(). 2nd Edit Looks like you have to call the method to create the temp directories if you don't want them in the @Test methods. Also make sure you close any files you open in the temp directory or they won't be automatically deleted.