title: How to abstract away java.time.Clock for testing purposes in Spring
body: <p>I have a question with regards to the question <a href="https://stackoverflow.com/questions/5622194/time-dependent-unit-tests">Time dependent unit tests</a></p>
  
  <p>Let's say I build Spring application which contains service interface and its implementation</p>
  
  <p>If I want to change clock in test, I would have to "pollute" production code and interface with e.g. <code>setClock</code> method as follows:</p>
  
  <pre><code>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;
      }
  }   
  </code></pre>
  
  <p>In test, I can invoke, e.g.:</p>
  
  <pre><code>service.setClock(Clock.fixed(Instant.parse("2017-02-14T01:22:00Z"), ZoneOffset.UTC));
  </code></pre>
  
  <p>How can I abstract away such cross-cutting concern in Spring?</p>
  
  <p>I want to stick with java.time.Clock (I don't want to use Joda)</p>