ClockService

This service allows an application to be decoupled from the system time. The most common use case is in support of testing scenarios, to "mock the clock". Use of this service also opens up the use of centralized co-ordinated time management through a centralized time service.

API

ClockService.java
class ClockService {
  VirtualClock getClock()
  long getEpochMillis()
}

Implementation

This service is concrete, so is its own implementation.

Usage

Most applications deal with dates and times in one way or another. For example, if an Order is placed, then the Customer may have 30 days to pay the Invoice, otherwise a penalty may be levied.

However, such date/time related functionality can quickly complicate automated testing: "today+30" will be a different value every time the test is run.

Even disregarding testing, there may be a requirement to ensure that date/times are obtained from an NNTP server (rather than the system PC). While instantiating a java.util.Date to current the current time is painless enough, we would not want complex technical logic for querying an NNTP server spread around domain logic code.

Therefore it’s common to provide a domain service whose responsibility is to provide the current time. This service can be injected into any domain object (and can be mocked out for unit testing). Apache Causeway provides such a facade through the ClockService.

See also