Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Rewrite this to be testable:

Java
class Coupon {
boolean isExpired() {
return LocalDate.now().isAfter(expiry);
}
}

Show how a test would set the date.

We inject a java.time.Clock into Coupon, defaulting to Clock.systemDefaultZone() or Clock.systemUTC() in production, and call LocalDate.now(clock) instead of LocalDate.now(). A test then passes Clock.fixed(Instant.parse("2025-01-01T00:00:00Z"), ZoneOffset.UTC) to pin today's date, or Clock.offset to move time forward from a base clock. In Spring, we define a Clock bean once and inject it wherever code needs the current time.

Why this question is asked

Code that reads the current time directly is one of the most common reasons tests are flaky or missing. A test for isExpired() written today passes until the expiry date arrives, and then fails in CI one morning for no visible reason. Interviewers ask this small refactoring because it shows whether we know the standard tool for it, java.time.Clock, and whether we understand the zone that comes with it. In this answer, we will see why the original code cannot be tested, introduce Clock, rewrite Coupon, write tests with a fixed and an offset clock, and wire the clock in Spring.

1Why the Original Code Cannot Be Tested

Here is the class from the question, with a constructor added.

Java
class Coupon {
private final LocalDate expiry;
Coupon(LocalDate expiry) { this.expiry = expiry; }
boolean isExpired() {
return LocalDate.now().isAfter(expiry); // reads the system clock in the JVM default zone
}
}
@Test
void notExpiredYet() {
assertFalse(new Coupon(LocalDate.of(2025, 6, 30)).isExpired()); // passes until 1 July 2025, then fails
}

LocalDate.now() has two hidden inputs: the real system clock and the JVM's default time zone. The test cannot control either. So it can only test 'today', its result changes with the calendar, and it can even differ between a developer in Tokyo and a CI server in Virginia on the same evening. We cannot write a test for 'expired the day after expiry' at all.

2A Quick Recap of java.time.Clock

java.time.Clock is a small abstract class whose job is to answer two questions: what is the current instant, and which time zone should be used to turn it into a date or a time? Every now() method in java.time has an overload that takes a Clock: Instant.now(clock), LocalDate.now(clock), ZonedDateTime.now(clock). The no-argument versions simply use the system clock with the default zone.

Java provides the implementations we need. Clock.systemUTC() and Clock.systemDefaultZone() read the real clock. Clock.fixed(instant, zone) always returns the same instant, which is perfect for tests. Clock.offset(base, duration) returns another clock's time shifted by a fixed amount, which lets a test say 'the same as before, but 40 days later'.

3Rewriting Coupon With an Injected Clock

We pass the clock in through the constructor and use it in the one place that needs the current date.

Java
class Coupon {
private final LocalDate expiry;
private final Clock clock;
Coupon(LocalDate expiry, Clock clock) {
this.expiry = expiry;
this.clock = clock;
}
Coupon(LocalDate expiry) {
this(expiry, Clock.systemDefaultZone()); // production default; Clock.systemUTC() if dates are UTC
}
boolean isExpired() {
return LocalDate.now(clock).isAfter(expiry);
}
}

Notice that the behaviour is unchanged for existing callers: the one-argument constructor still uses the system clock and the default zone. But the choice of default now deserves a thought. LocalDate.now(clock) uses the clock's zone to decide what today is, so Clock.systemUTC() means 'expires at midnight UTC' and Clock.systemDefaultZone() means 'midnight wherever the server is'. A coupon for customers in one country might really want a clock in that country's zone, such as Clock.system(ZoneId.of("Europe/Paris")).

4Setting the Date in a Test

Now a test can decide exactly what today is. We fix the clock to a moment, and the date follows from the clock's zone.

Java
Clock newYear = Clock.fixed(Instant.parse("2025-01-01T00:00:00Z"), ZoneOffset.UTC);
Coupon coupon = new Coupon(LocalDate.of(2025, 1, 1), newYear);
assertFalse(coupon.isExpired()); // today 2025-01-01 is not after the expiry date
Clock nextDay = Clock.offset(newYear, Duration.ofDays(1));
assertTrue(new Coupon(LocalDate.of(2025, 1, 1), nextDay).isExpired()); // today 2025-01-02
Clock newYork = newYear.withZone(ZoneId.of("America/New_York"));
System.out.println(LocalDate.now(newYork)); // 2024-12-31: same instant, different date
What isExpired() sees with each clock
Clock in the testLocalDate.now(clock)isExpired() for expiry 2025-01-01
fixed 2025-01-01T00:00Z, UTC2025-01-01false
offset by 1 day2025-01-02true
same instant, America/New_York2024-12-31false

The first two assertions test the boundary that matters: a coupon is still valid on its expiry day and expired the day after. These tests will give the same result next year and on any machine. The last line shows why the zone is part of the clock: the same instant is still 31 December in New York, so the same coupon would not yet be at its expiry day there.

5Wiring the Clock in Spring

In a Spring application, we do not want every class to construct its own clock. We define one Clock bean in a configuration class and let Spring inject it, so production code shares a single source of time, and tests replace that one bean.

Java
@Configuration
class TimeConfig {
@Bean
Clock clock() { return Clock.systemUTC(); }
}
@Service
class CouponService {
private final Clock clock;
CouponService(Clock clock) { this.clock = clock; }
boolean isExpired(Coupon c) { return LocalDate.now(clock).isAfter(c.expiry()); }
}
// In a Spring Boot test
@TestConfiguration
class FixedClockConfig {
@Bean @Primary
Clock fixedClock() { return Clock.fixed(Instant.parse("2025-01-01T00:00:00Z"), ZoneOffset.UTC); }
}

Notice that the service version takes the clock in the service rather than in the coupon. Coupon might be a JPA entity or a record created from JSON, where constructor injection of a clock is awkward. An even simpler design for such value objects is a pure method that receives today's date, isExpiredOn(LocalDate today), and leaves the clock to the service that calls it.

6Follow-up questions

Why not mock LocalDate.now() with a mocking library instead?

Some libraries can mock static methods, but it is slower, more fragile and hides the dependency. A Clock parameter makes the time dependency visible in the constructor, works with any test framework, and needs no special tooling. It is also exactly what the java.time designers intended Clock for.

Is it a problem that Clock.fixed never moves?

Only for tests that need time to pass during the test. For those, we use Clock.offset to create a later clock, or a small mutable test clock that we can advance. For most date logic, a fixed clock per test case is the clearest choice.

Which zone should the production clock use?

The zone that defines 'today' for the business rule. Services often use Clock.systemUTC() as the bean and apply the customer's or market's zone explicitly with clock.withZone(zone) when a rule depends on local dates. What matters is that the choice is explicit rather than taken from the server.

7Conclusion

In this answer, we saw that LocalDate.now() hides two inputs, the system clock and the default zone, so isExpired() could not be tested reliably. Injecting a java.time.Clock and calling LocalDate.now(clock) made both explicit. Tests use Clock.fixed to set today and Clock.offset to move forward, and the clock's zone decides which date an instant falls on. In Spring, a single Clock bean provides time to the whole application and a test configuration replaces it.

What to say in the interviewAbout 70 seconds aloud

Open with

The problem is that LocalDate.now() reads two things we can't control in a test: the system clock and the JVM's default time zone. So I'd inject a java.time.Clock into Coupon through the constructor and call LocalDate.now(clock) instead.

Then, if they want more

For existing callers I'd keep a constructor that defaults to Clock.systemDefaultZone(), or Clock.systemUTC() if the business defines dates in UTC, because the clock's zone decides what today is. In the test I'd create Clock.fixed with Instant.parse of 2025-01-01T00:00:00Z and ZoneOffset.UTC, and a coupon expiring on January 1st, and assert it isn't expired on that day.

Then I'd use Clock.offset of that clock by one day and assert it is expired, so I test the boundary. I'd also point out that the same instant with an America/New_York zone is still December 31st, which is exactly why the zone belongs in the clock.

In Spring I'd define one Clock bean, inject it into services, and override it with a fixed clock in tests using a test configuration. If Coupon is an entity or a record, I might keep it pure with a method like isExpiredOn(today) and let the service pass LocalDate.now(clock).

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Rewrite this to be testable:": https://scrivio-interview-library.netlify.app/java/date-and-time/45445b8e9b/