我用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)