4

Regarding date dependent unit tests I am figuring out the right way of creating a Java 8 Clock for a specific date. According to the approach proposed in Unit testing a class with a Java 8 Clock, I am trying it with the Clock.fixed method. However, I am not realizing a short way of doing it.

Is this the right and best way of creating a Java 8 Clock for a specific date?

version 2 (according to suggestions of @Meno, @LakiGeri and @basil-bourque)

private static Clock utcClockOf(int year, int month, int day) {
    ZoneOffset offset = ZoneOffset.UTC;
    Instant inst = LocalDate
            .of(year, month, day)
            .atStartOfDay(offset)
            .toInstant();
    return Clock.fixed(inst, offset.normalized());
}

version 1

static Clock clockOf(int year, int month, int day) {
    LocalDate now = LocalDate.of(year, month, day);
    long days = now.toEpochDay();
    long secs = days*24*60*60;
    Instant inst = Instant.ofEpochSecond(secs);
    return Clock.fixed(inst, ZoneId.systemDefault());
}
Community
  • 1
  • 1
Miguel Gamboa
  • 8,855
  • 7
  • 47
  • 94
  • why dont you use this: `now.atStartOfDay().toInstant(ZoneOffset.UTC);` to create instant? On the other hand, LocalDate not handle hour / minute / sec.. I recommend you to use `LocalDateTime` or `ZonedDateTime` – LakiGeri Apr 06 '17 at 11:24
  • 1
    A clock is not designed to yield a calendar date but an instant. If you just want to have an injection mechanism for calendar dates then you might consider the interface `java.util.function.Supplier`. Otherwise - in case of a `Clock`-implementation, you have to think about timezone effects i.e. specifying which explicit timezone you want else your "fixed" clock would not really be fixed. – Meno Hochschild Apr 06 '17 at 11:59
  • why do you need to create a fake Clock? – holi-java Apr 06 '17 at 13:12
  • @holi-java Are you suggesting to mock Clock with an auxiliary mocking library, such as Mockito? – Miguel Gamboa Apr 06 '17 at 13:38
  • @LakiGeri I will use your suggestions to create an instant. – Miguel Gamboa Apr 06 '17 at 13:43
  • 1
    @MenoHochschild I will consider the timezone when creating a Clock instance and I updated the auxiliary function `clockOf()` to `utcClockOf()` – Miguel Gamboa Apr 06 '17 at 13:45
  • 2
    @MiguelGamboa I think you maybe miss something in your domain language. There is a OO princple: "Tell, don't ask". – holi-java Apr 06 '17 at 14:04
  • @holi-java Sorry, but I am not understanding your point of view. I am aware of the "Tell, don't ask" principle, but I am not getting your idea. – Miguel Gamboa Apr 06 '17 at 15:02
  • @MiguelGamboa chpater20:listen to the test in [GOOS](http://www.growing-object-oriented-software.com/) there is an example in section 2 & 3.you can download GOOS from [this](http://www.cs.umss.edu.bo/doc/material/mat_gral_137/Addison.Wesley.Growing.Object.Oriented.Software.Guided.by.Tests.Oct.2009%20(1).pdf). – holi-java Apr 06 '17 at 15:10
  • 1
    Pass that `offset` to `atStartOfDay` rather than `toInstant` – Basil Bourque Apr 10 '17 at 18:59

0 Answers0