Date relative to current in the DBUnit dataset
Asked Answered
A

4

15

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?

Ardel answered 18/5, 2010 at 11:41 Comment(0)
D
25

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.

Dextrogyrate answered 18/6, 2010 at 18:37 Comment(2)
Thanks for such detailed explanation. It's really useful.Ardel
where do i need to add the source code in my junit? in @Before method ?Holtz
K
4

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:

  • [now] : current date time
  • [now-1d] : the same time yesterday
  • [now+1y+1M-2h] : a year and a month from today, two hours earlier
  • [now+1d 10:00] : 10 o'clock tomorrow
Kleeman answered 28/4, 2020 at 14:55 Comment(1)
That should be the right answer now! Thanks!Syllabize
D
1

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:

@Inject
protected DataSource dataSource;

@Before
public void setUp() throws Exception {
        DataSourceDatabaseTester dataSourceDatabaseTester = new DataSourceDatabaseTester(dataSource);
        IDataSet dataSet = new FlatXmlDataSetBuilder().build(new FileInputStream(getClass().getResource(DATASET_FILE_LOCATION).getPath()));
        ReplacementDataSet rDataSet = new ReplacementDataSet(dataSet);
        rDataSet.addReplacementObject("{$today}", new Date());
        dataSourceDatabaseTester.setDataSet(rDataSet);
        dataSourceDatabaseTester.onSetup(); 
}

This seemed to do the trick

Dysteleology answered 23/9, 2019 at 15:28 Comment(0)
P
0

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.

Perturb answered 18/5, 2010 at 12:1 Comment(1)
Yes I know how to calculate relative day in Java. It looks like I really need to parse dataset and calculate dates manually.Ardel

© 2022 - 2024 — McMap. All rights reserved.