java 相对于 DBUnit 数据集中当前的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2856840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:10:33  来源:igfitidea点击:

Date relative to current in the DBUnit dataset

javaunit-testingdbunit

提问by VestniK

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.

我想知道是否有任何方法可以将明天指定为 DBUnit XML 数据集中的日期。有时,未来日期和过去日期的代码逻辑不同,我想测试这两种情况。当然,我可以指定类似 2239 年 11 月 5 日的日期,并确保该测试将在此日期之前有效,但还有更优雅的方式。

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.

我在Java开发过程中还没有遇到过这种情况,但是我曾经遇到过日期前一天、日期前两天和日期前两天以上的代码逻辑不同的情况。在这种情况下,编写数据库驱动测试的唯一可能解决方案是在数据导入期间插入相关日期。

Are there any facilities provided by the DBUnit for this?

DBUnit 是否为此提供了任何设施?

回答by loyalBrown

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.

我刚刚开始使用 DBUnit 并正在寻找类似的功能。不幸的是,框架中似乎没有日期的表达语言。但是,我确实找到了使用 DBUnit 的 ReplacementDataSet 类的合适解决方法。此类采用 IDataSet 对象并公开用于替换由 IDataSet 对象从数据集文件中提取的对象的方法。

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.

希望这可以帮助。祝你好运。

回答by Henrique

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")

我已经设法通过与@loyalBrown 所做的非常相似的事情来实现这一点,但是我无法完全那样做,因为那里缺少一些进一步的信息,并且我正在使用 @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:

首先,我需要删除注释,您现在需要使用以下代码以编程方式启动此 .xml 文件:

@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

这似乎成功了

回答by ave

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.

DbUnit 2.7.0 中添加了新的相对日期/时间语法,您现在可以编写[now+1d]以设置明天的日期,而无需任何额外设置。

<dataset>
  <user create_date="[now+1d]"/>
<dataset>

The doc is here:
http://dbunit.sourceforge.net/datatypes.html#relativedatetime

文档在这里: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
  • [now] : 当前日期时间
  • [now-1d] : 昨天同一时间
  • [now+1y+1M-2h] : 从今天起一年零一个月,提前两个小时
  • [now+1d 10:00] : 明天10点

回答by khmarbaise

You can use add() of Calendarto 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.

您可以使用Calendar 的 add()来定义未来的日期,并将其与 JUnit 的数据源结合使用。我怀疑这是否适用于 DBUnit 的 XML 格式。可能是您创建自己的 TestCase,它从 DBTestCase 扩展并实现 getDataSet() 方法。