Mocking value objects is rarely a good idea. You should mock behaviour, not data. I guess what you really want to achieve is to be able to use arbitrary date in tests (which is a good idea).
The pattern I am using successfully in such cases is a fake system clock (examples from that site):
public interface TimeSource {
long currentTimeMillis();
}
And two implementations, real:
public final class RealSource implements TimeSource {
public long currentTimeMillis() {
return System.currentTimeMillis();
}
}
and fake for testing:
public final class FakeSource implements TimeSource {
public long currentTimeMillis() {
return //...whatever you want
}
}
I find it convenient to make currentTimeMillis()
static in a helper method and use static
field pointing to current TimeSource
.
Finally, much simpler approach is to pass the date directly:
class Lekcja {
Calendar _date;
public Lekcja(Calendar date) {
this._date = date;
}
//...
}
See also