I have a question with regards to the question Time dependent unit tests
Let's say I build Spring application which contains service interface and its implementation
If I want to change clock in test, I would have to "pollute" production code and interface with e.g. setClock
method as follows:
public interface MyService {
void heavyBusinessLogic();
void setClock(Clock clock);
}
@Service
public class MyServiceImpl implements MyService {
private Clock clock = Clock.systemDefaultZone();
@Override
public void heavyBusinessLogic() {
if (LocalDate.now(clock)...) {
...
}
}
@Override
public void setClock(Clock clock) {
this.clock = clock;
}
}
In test, I can invoke, e.g.:
service.setClock(Clock.fixed(Instant.parse("2017-02-14T01:22:00Z"), ZoneOffset.UTC));
How can I abstract away such cross-cutting concern in Spring?
I want to stick with java.time.Clock (I don't want to use Joda)