相对于 DBUnit 数据集中当前的日期
我想知道是否有任何方法可以指定例如明天作为 DBUnit XML 数据集中的日期。有时,未来日期和过去日期的代码逻辑是不同的,我想测试这两种情况。当然,我可以指定 2239 年 11 月 5 日之类的日期,并确保测试在此日期之前有效,但是否有更优雅的方法。
我在Java开发过程中还没有遇到过这种情况,但是我曾经遇到过代码逻辑在日期前一天、日期前两天和日期前两天以上不同的情况。在这种情况下,编写数据库驱动测试的唯一可能的解决方案是在数据导入期间插入相对日期。
DBUnit 是否为此提供了任何设施?
I'm wondering if there is any way to specify for example tomorrow as date in the DBUnit XML dataset. Sometimes code logic is different for dates in future and dates in the past and I want to test both cases. For sure I can specify something like the 5th of November 2239 and be sure that test will work till this date but are there more elegant way.
I haven't yet faced such situation during my Java development but once I had experience when code logic was different for one day before dates, two days before dates and more than two days before dates. In this case the only possible solution to write database driven test is to insert relative dates during data import.
Are there any facilities provided by the DBUnit for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我刚刚开始使用 DBUnit,并正在寻找类似的功能。不幸的是,框架中似乎没有日期的表达语言。不过,我确实使用 DBUnit 的 ReplacementDataSet 类找到了合适的解决方法。此类采用 IDataSet 对象并公开方法来替换 IDataSet 对象从数据集文件中提取的对象。
数据集
源代码
现在,当测试运行时,用户的创建数据将始终设置为测试运行前的两天。
希望这有帮助。祝你好运。
I just started using DBUnit and was looking for similar capabilities. Unfortunately there doesn't seem to be an expression language for dates in the framework. However, I did find a suitable workaround using DBUnit's ReplacementDataSet class. This class takes an IDataSet object and exposes methods to replace objects extracted by the IDataSet object from the data set files.
dataset
source code
Now, when the test runs the user's creation data will always be set to two days before the test was run.
Hope this helps. Good luck.
DbUnit 2.7.0 中添加了新的相对日期/时间语法,您现在可以编写
[now+1d]
来设置明天的日期,而无需任何额外的设置。该文档在这里:
http://dbunit.sourceforge.net/datatypes.html#relativedatetime
一些示例来自文档:
A new relative date/time syntax has been added in DbUnit 2.7.0 and you can now write
[now+1d]
to set tomorrow's date without any extra setup.The doc is here:
http://dbunit.sourceforge.net/datatypes.html#relativedatetime
A few examples from the doc:
我已经设法通过与 @loyalBrown 所做的非常相似的事情来实现这一目标,但我无法完全像那样做,因为那里缺少一些进一步的信息,并且我正在使用 @DatabaseSetup("/pathToXML") 实例化我当前的数据源
所以这就是我所做的:
首先我需要删除注释,现在您需要使用以下代码以编程方式启动此 .xml 文件:
这似乎可以解决问题
I've managed to achieve that with something really similar to what @loyalBrown did, but I couldn't do exactly like that as some further information was missing there and I was instantiating my current datasource with @DatabaseSetup("/pathToXML")
So that's what I did:
First I needed to remove the annotation, you need now to start this .xml file programatically with the following code:
This seemed to do the trick
您可以使用 Calendar 的 add() 来定义未来的日期,并将其与 JUnit 的数据源联系起来使用。我怀疑这是否适用于 DBUnit 的 XML 格式。也许您创建自己的 TestCase,它从 DBTestCase 扩展并实现 getDataSet() 方法。
You can use add() of Calendar to define dates in the future and using this in relationship with datasource for JUnit. I doubt that this would work with DBUnit's XML format. May be you create your own TestCase which extends from DBTestCase and implement getDataSet() method.