java 如何模拟 JodaTime 的实际日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4454106/
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-30 06:27:58  来源:igfitidea点击:

How can I mock JodaTime actual date?

javatestingmockingjodatime

提问by Adriano Bacha

I want to test this method:

我想测试这个方法:

 public FirmOrder findActiveByModelColor(ModelColor modelColor) {
   Query query = em.createQuery("FROM FirmOrder fo WHERE fo.modelColor = :modelColor AND fo.year = :year AND fo.month = :month");
   query.setParameter("modelColor", modelColor);
   query.setParameter("year", new DateTime().year().get());
   query.setParameter("month", new DateTime().monthOfYear().get());
   return (FirmOrder) query.getSingleResult();
 }

but I need DateTime().year().get()and DateTime().dayOfMonth().get()to always return the same date

但我需要DateTime().year().get()并且DateTime().dayOfMonth().get()总是返回相同的日期

tks

tks

回答by axtavt

If you can't add a factory object as suggested by skaffman, you can use DateTimeUtils.setCurrentMillisFixed().

如果您无法按照 skaffman 的建议添加工厂对象,则可以使用DateTimeUtils.setCurrentMillisFixed().

回答by skaffman

Then you need to define a Clockinterface, and inject it into your class

然后你需要定义一个Clock接口,并将其注入到你的类中

public interface Clock {
    DateTime getCurrentDateTime();
}

then:

然后:

Clock clock;

public FirmOrder findActiveByModelColor(ModelColor modelColor) {
   Query query = em.createQuery("FROM FirmOrder fo WHERE fo.modelColor = :modelColor AND fo.year = :year AND fo.month = :month");
   query.setParameter("modelColor", modelColor);
   query.setParameter("year", clock.getCurrentDateTime().year().get());
   query.setParameter("month", clock.getCurrentDateTime().dayOfMonth().get());
   return (FirmOrder) query.getSingleResult();
 }

Your test can then inject an implementation of Clock(e.g. using a mocking framework) that always returns a fixed time.

然后,您的测试可以注入一个Clock始终返回固定时间的实现(例如使用模拟框架)。

I use the Clockinterface a lot in my own stuff, and I remain surprised that it's not part of one of the common libraries out there. I have two implementations I use a lot, WallClockand StoppedClock(which is useful for tests that use a fixed time).

Clock在自己的东西中经常使用该界面,但我仍然惊讶于它不是现有公共库之一的一部分。我有两个我经常使用的实现,WallClock并且StoppedClock(这对于使用固定时间的测试很有用)。

回答by Behrang Saeedzadeh

Looks like the only option is to use the answer feature to post this comment:

看起来唯一的选择是使用答案功能发布此评论:

The following portion of your code can lead to hard-to-detect errors:

代码的以下部分可能会导致难以检测的错误:

query.setParameter("year", new DateTime().year().get());
query.setParameter("month", new DateTime().monthOfYear().get());

Let's pretend tha today is the last day of the year 2011 and this part of code is called 1 nano second prior to the new year and that the first statement takes more than 1 nano second to complete. This means that year will be set to 2011 but month to 1 but it had to best either to 2011/12 or 2012/1.

让我们假设今天是 2011 年的最后一天,这部分代码在新年前被称为 1 纳秒,并且第一个语句需要超过 1 纳秒才能完成。这意味着年份将设置为 2011 年,但月份为 1,但它必须最好设置为 2011/12 或 2012/1。

Though that statistically it is very unlikely to happen, but logically it can happen :)

虽然从统计上来说它不太可能发生,但从逻辑上讲它可能会发生:)

You should create one DateTime instance and use that to populate both of yearand month.

您应该创建一个 DateTime 实例并使用它来填充yearmonth

回答by Rogério

It's easy, if using the JMockitExpectations mocking API:

如果使用JMockitExpectations 模拟 API ,这很容易:

@Test
public void findActiveByModelColor()
{
    new NonStrictExpectations()
    {
        @Cascading DateTime dt;

        {
            dt.year().get(); result = 2010;
            dt.monthOfYear().get(); result = 12;
        }
    };

    FirmOrder fo = testedObject.findActiveByModelColor(modelColor);

    // asserts...
}