时间相关单元测试 [英] Time dependent unit tests

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

问题描述

我需要测试一个函数,其结果将取决于当前时间(使用 Joda 时间的 isBeforeNow(),它确实发生了).

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

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

是否可以使用(例如,使用 Mockito)存根/模拟系统时间,以便我可以可靠地测试该功能?

Is it possible to stub/mock out the system time with (using Mockito, for example) so that I can reliably test the function?

推荐答案

Joda 时间支持通过 setCurrentMillisFixedsetCurrentMillisOffset 方法设置假"当前时间code>DateTimeUtils 类.

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

请参阅 https://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeUtils.html

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

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