Date relative to current in the DBUnit dataset

前端 未结 4 1338
情话喂你
情话喂你 2021-02-04 08:09

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

4条回答
  •  -上瘾入骨i
    2021-02-04 08:21

    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

    
        
    
    

    source code

    String dataSetFile = "testDataFile.xml";
    IDataSet dataSet = new FlatXmlDataSetBuilder().build(new FileInputStream(dataSetFile));
    ReplacementDataSet rDataSet = new ReplacementDataSet(dataSet);
    Set 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.

提交回复
热议问题