I have a function that uses the current time to make some calculations. I\'d like to mock it using mockito.
An example of the class I\'d like to test:
One approach, that does not directly answer the question but might solve the underlying problem (having reproducible tests), is allow the Date
as an parameter for tests and add a delegate to the default date.
Like so
public class ClassToTest {
public long getDoubleTime() {
return getDoubleTime(new Date());
}
long getDoubleTime(Date date) { // package visibility for tests
return date.getTime() * 2;
}
}
In production code, you use getDoubleTime()
and test against getDoubleTime(Date date)
.