DBUnit数据集中与当前相关的日期 [英] Date relative to current in the DBUnit dataset

查看:116
本文介绍了DBUnit数据集中与当前相关的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何方法可以在DBUnit XML数据集中明确指定日期作为日期。有时代码逻辑在将来的日期和过去的日期是不同的,我想测试这两种情况。当然,我可以指定像2239年11月5日这样的东西,并确保测试在这个日期之前有效,但有更优雅的方式。

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.

我还没有面对在我的Java开发过程中遇到这种情况,但有一次我遇到过代码逻辑在日期前一天,日期前两天和日期前两天不同的经历。在这种情况下,编写数据库驱动测试的唯一可能解决方案是在数据导入期间插入相对日期。

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.

DBUnit是否为此提供了任何工具?

Are there any facilities provided by the DBUnit for this?

推荐答案

我刚刚开始使用DBUnit并且正在寻找类似的功能。不幸的是,框架中的日期似乎没有表达式语言。但是,我确实找到了使用DBUnit的ReplacementDataSet类的合适解决方法。此类接受一个I​​DataSet对象,并公开方法以替换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>
    <user first_name="Dan"
          last_name="Smith"
          create_date="[create_date]"/>
<dataset>

源代码

String dataSetFile = "testDataFile.xml";
IDataSet dataSet = new FlatXmlDataSetBuilder().build(new FileInputStream(dataSetFile));
ReplacementDataSet rDataSet = new ReplacementDataSet(dataSet);
Set<String> keys = dataSetAdjustments.keySet();
rDataSet.addReplacementObject("[create_date]", DateUtils.addDays(new Date(), -2));

现在,当测试运行时,用户的创建数据将始终设置为测试前两天跑。

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数据集中与当前相关的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆