我需要测试一个函数,其结果将取决于当前时间(使用Joda时间isBeforeNow()).
public boolean isAvailable() {
return (this.someDate.isBeforeNow());
}
Run Code Online (Sandbox Code Playgroud)
是否可以使用Mockito存根/模拟系统时间,以便我可以可靠地测试功能?
我用JodaTime#DateTime,我需要嘲笑它的行为.由于无法直接模拟JodaTime#DateTime,我创建了它的接口
Clock.java
public interface Clock {
DateTime getCurrentDateTimeEST();
DateTime getFourPM_EST();
DateTime getSevenPM_EST();
}
Run Code Online (Sandbox Code Playgroud)
JodaTime.java
public class JodaTime implements Clock {
@Override
public DateTime getCurrentDateTimeEST() {
return new DateTime(DateTimeZone.forID("EST"));
}
@Override
public DateTime getFourPM_EST() {
DateTime current = getCurrentDateTimeEST();
return new DateTime(current.getYear(), current.getMonthOfYear(),
current.getDayOfMonth(), 16, 0, 0, 0, DateTimeZone.forID("EST"));
}
@Override
public DateTime getSevenPM_EST() {
DateTime current = getCurrentDateTimeEST();
return new DateTime(current.getYear(), current.getMonthOfYear(),
current.getDayOfMonth(), 19, 0, 0, 0, DateTimeZone.forID("EST"));
}
}
Run Code Online (Sandbox Code Playgroud)
这是我想测试的方法
public class PrintProcessor{
Clock jodaTime; …Run Code Online (Sandbox Code Playgroud)