Is there any way to manipulate the current time in a jUnit 4.5 test? I have the following method which I'd like to have a unit test for
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);
}
One way to make it easier to test is to split it into two methods
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);
}
That lets me test the week subtraction logic, but leaves getLastWeek untested and I prefer to have just one method for this.