Java – relative to the current date in the Dbunit dataset

I wonder if there is any way to specify, for example, tomorrow as the date in the Dbunit XML dataset Sometimes the code logic is different between the future date and the past date. I want to test both cases Make sure I can specify something like November 5, 2239 and make sure the test works until this date, but there is a more elegant way

I haven't encountered such a situation in java development, but once I have experience in code logic one day before the date, two days before the date and more than two days before the date In this case, the only possible solution for writing database - driven tests is to insert relevant dates during data import

Are there any tools provided by Dbunit for this?

Solution

I just started using Dbunit and am looking for similar features Unfortunately, the date in the framework doesn't seem to be an expression language However, I can't find a suitable solution by using the replacementdataset class of Dbunit This class accepts an idataset object and exposes methods to replace the object extracted by the idataset object from the dataset file

data set

<dataset>
    <user first_name="Dan"
          last_name="Smith"
          create_date="[create_date]"/>
<dataset>

source code

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 running the test

I hope this will help Good luck.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>