16

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)

3
  • Why not use constructor injection instead? Then there is no setClock() method in the interface
    – Shiraaz.M
    May 2, 2017 at 13:33
  • java.time.Clock is not a bean May 2, 2017 at 13:34
  • 3
    ...which is not a problem, since you could define a Clock bean in your productive configuration and a mocked one in your test configuration... May 2, 2017 at 13:37

2 Answers 2

27

Personally, I would simply add the clock in the constructor...

public MyServiceImpl(Clock clock) {
  this.clock = clock;
}

...and perhaps add a nice default constructor...

public MyServiceImpl() {
  this(Clock.systemDefaultZone());
}

This way you can get the default thing via spring and create a custom clock version manually, for example in your tests.

Of course, you could also forgo the default constructor and simply add a Clock bean in your productive configuration, for example like this...

@Bean
public Clock clock() {
 return Clock.systemDefaultZone();
}

...which allows you to use a mocked Clock as a bean in your test configuration, automatically allowing Spring to @Autowire it via constructor injection.

7
  • I just came across another problem. I'd like to use clock for time sensitive comparisons in entity itself and I think injecting Clock bean is not good idea. What do you suggest? May 5, 2017 at 7:45
  • 2
    Why do you think it isn't a good idea? I don't quite see the problem with that... May 5, 2017 at 8:12
  • It requires either aspectj load time weaving or Hibernate LoadEventListener. I thought there is a more standardized way to do it :) May 5, 2017 at 8:16
  • Definitely go for the bean approach. Then you can write, say, a SpringBootTest and use a test configuration to autowire any clock you desire into the entire app with no other changes. There are some very useful Clock implementations specifically made for testing. Mar 5 at 15:12
  • @PatrikMihalčin a Clock is an I/O component. Would you access a file system or a network connection from inside your entity? I'd suggest keeping the clock in your service and let the entity see only the times it produces. Mar 5 at 15:18
0

Good way how to model clock is to use ThreadLocal, especially when you need it in JPA entity, e.g.:

@Entity
public class MyEntity {
   public static final ThreadLocal<Clock> CLOCK =
            ThreadLocal.withInitial(Clock::systemDefaultZone);
}
1
  • IIRC, Java 8 date-time classes are thread-safe, so simple statically initialized holder could be used. Aug 16, 2021 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.