相关疑难解决方法(0)

时间依赖单元测试

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

    public boolean isAvailable() {
    return (this.someDate.isBeforeNow());
}
Run Code Online (Sandbox Code Playgroud)

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

java junit

72
推荐指数
3
解决办法
4万
查看次数

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 ×2

junit ×2

jodatime ×1

mockito ×1