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?
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
<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 the test was run.
Hope this helps. Good luck.
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.
<dataset>
<user create_date="[now+1d]"/>
<dataset>
The doc is here:
http://dbunit.sourceforge.net/datatypes.html#relativedatetime
A few examples from the doc:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With