有没有办法在jUnit 4.5测试中操纵当前时间?我有以下方法,我想进行单元测试
public String getLastWeek() {
GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone("Europe/Stockholm"));
c.setFirstDayOfWeek(GregorianCalendar.MONDAY);
c.add(GregorianCalendar.WEEK_OF_YEAR,-1);
return c.get(GregorianCalendar.YEAR) + " " + c.get(GregorianCalendar.WEEK_OF_YEAR);
}
使测试更容易的一种方法是将其拆分为两种方法
public String getLastWeek() {
GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone("Europe/Stockholm"));
return getLastWeekFor(c);
}
public String getLastWeekFor(GregorianCalander c) {
c.setFirstDayOfWeek(GregorianCalendar.MONDAY);
c.add(GregorianCalendar.WEEK_OF_YEAR,-1);
return c.get(GregorianCalendar.YEAR) + " " + c.get(GregorianCalendar.WEEK_OF_YEAR);
}
这让我测试了周减法逻辑,但是让getLastWeek没有经过测试,我更喜欢只有一种方法.