相关疑难解决方法(0)

Mockito:如何模拟JodaTime的界面

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

java junit jodatime mockito

11
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×1

jodatime ×1

junit ×1

mockito ×1