时间依赖单元测试 [英] Time dependent unit tests

查看:171
本文介绍了时间依赖单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测试一个函数,其结果将取决于当前时间(使用Joda时间的 isBeforeNow())。

I need to test a function that whose result will depend on current time (using Joda time's isBeforeNow()).

    public boolean isAvailable() {
    return (this.someDate.isBeforeNow());
}

是否可以使用Mockito存根/模拟系统时间以便我可以可靠地测试函数吗?

Is it possible to stub/mock out the system time with Mockito so that I can reliably test the function?

推荐答案

Joda时间支持通过 setCurrentMillisFixed设置假当前时间 setCurrentMillisOffset DateTimeUtils 类的方法。

Joda time supports setting a "fake" current time through the setCurrentMillisFixed and setCurrentMillisOffset methods of the DateTimeUtils class.

请参阅 http://joda-time.sourceforge.net /api-release/org/joda/time/DateTimeUtils.html

这篇关于时间依赖单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆