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

发布于 2024-09-02 10:06:12 字数 274 浏览 6 评论 0原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

走走停停 2024-09-09 10:06:12

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

数据集

<dataset>
    <user first_name="Dan"
          last_name="Smith"
          create_date="[create_date]"/>
<dataset>

源代码

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

现在,当测试运行时,用户的创建数据将始终设置为测试运行前的两天。

希望这有帮助。祝你好运。

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.

生寂 2024-09-09 10:06:12

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

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

该文档在这里:
http://dbunit.sourceforge.net/datatypes.html#relativedatetime

一些示例来自文档:

  • [now] :当前日期时间
  • [now-1d] :昨天同一时间
  • [now+1y+1M-2h] :从今天起一年零一个月,早两个小时
  • [now+1d 10:00] : 明天 10 点

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
初相遇 2024-09-09 10:06:12

我已经设法通过与 @loyalBrown 所做的非常相似的事情来实现这一目标,但我无法完全像那样做,因为那里缺少一些进一步的信息,并且我正在使用 @DatabaseSetup("/pathToXML") 实例化我当前的数据源

所以这就是我所做的:

首先我需要删除注释,现在您需要使用以下代码以编程方式启动此 .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(); 
}

这似乎可以解决问题

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

人海汹涌 2024-09-09 10:06:12

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文