Date relative to current in the DBUnit dataset

前端 未结 4 1388
情话喂你
情话喂你 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条回答
  •  不要未来只要你来
    2021-02-04 08:21

    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

提交回复
热议问题