joda datetime测试心得

有些业务逻辑是基于时间的,测试起来比较麻烦,如果用joda datetime就很容易了

比如: 在new Report中有这样的逻辑:

public Report(double energyTotal, double outputEnergy, double outputpower, String clientId, ReportQueryType type) {
        this.clientId = clientId;
        this.energyTotal = energyTotal;
        this.outputEnergy = outputEnergy;
        this.outputpower = outputpower;
        this.reportDate = DateTime.now().withTimeAtStartOfDay().getMillis();
        this.monitorDate = DateTime.now().getMillis();
        this.reportType = type.toValue();
    }

 

字段的赋值用的是系统当前时间, 如何测试?

用joda datetime 时可以这样

// given
        DateTime date = now().withTimeAtStartOfDay();
        List<Report> hourlyReports = newArrayList();
        setCurrentMillisFixed(date.withHourOfDay(1).getMillis());
        hourlyReports.add(buildSimpleReport("inverter1", 200, 20, 10, DATE_TREND));
        setCurrentMillisFixed(date.withHourOfDay(2).getMillis());
        hourlyReports.add(buildSimpleReport("inverter1", 201, 21, 11, DATE_TREND));
        setCurrentMillisFixed(date.withHourOfDay(3).getMillis());
        hourlyReports.add(buildSimpleReport("inverter1", 202, 22, 12, DATE_TREND));
        setCurrentMillisSystem();

 

注意这里

setCurrentMillisFixed(......);指定当前系统时间的值

setCurrentMillisSystem();恢复当前系统时间为正常的系统时间.

 

 

 

 

你可能感兴趣的:(DateTime)