且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

DBUnit数据集中与当前相关的日期

更新时间:2023-02-27 13:11:40

我刚刚开始使用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.