Sub topic 17
Making Time-Dependent Code Testable with Clock
Some code depends on the current time. It might check whether a bill is late or whether a coupon has expired. That kind of code is hard to test, because "now" is different every time you run it. A test that passes today might fail next week.
Java solves this with the Clock class. A Clock is an object that tells java.time what "now" is. You can use the real clock when your program runs and a fake one in your tests.
Why `now()` Causes Trouble
When you call LocalDate.now() with no arguments, Java reads the computer's system clock. You cannot control that value. If your test expects a bill to be late, you would have to wait until the due date passes.
Every now() method in java.time also has a version that takes a Clock. Examples are LocalDate.now(clock), LocalDateTime.now(clock), and Instant.now(clock). If you pass in a clock, your code asks that clock for the time instead of the computer.
The Main Kinds of Clock
| Method | What it gives you | When to use it |
|---|---|---|
| `Clock.systemDefaultZone()` | The real time in the computer's time zone | Running the real program |
| `Clock.systemUTC()` | The real time in UTC | Servers that work in UTC |
| `Clock.fixed(instant, zone)` | A time that never changes | Tests |
| `Clock.offset(clock, duration)` | Another clock moved forward or back | Testing "an hour later" |
A fixed clock always returns the same instant. That makes your test give the same result on every run.
A Worked Example
The Invoice class below gets its clock through its constructor. The class never decides which clock to use. The caller does. This pattern is often called passing in a dependency.
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
public class Invoice {
private final LocalDate dueDate;
private final Clock clock;
public Invoice(LocalDate dueDate, Clock clock) {
this.dueDate = dueDate;
this.clock = clock;
}
public boolean isOverdue() {
return LocalDate.now(clock).isAfter(dueDate);
}
public static void main(String[] args) {
LocalDate due = LocalDate.of(2024, 1, 15);
Clock before = Clock.fixed(Instant.parse("2024-01-10T12:00:00Z"), ZoneOffset.UTC);
Clock after = Clock.fixed(Instant.parse("2024-01-20T12:00:00Z"), ZoneOffset.UTC);
System.out.println(new Invoice(due, before).isOverdue()); // false
System.out.println(new Invoice(due, after).isOverdue()); // true
Clock real = Clock.systemDefaultZone();
System.out.println(new Invoice(due, real).isOverdue()); // true (any date after 2024-01-15)
}
}The first two results never change, because both clocks are fixed. The last line uses the real clock, the same way your actual program would.
Common Mistakes
- Creating the clock inside the class. If
InvoicecalledClock.systemDefaultZone()itself, a test could not swap in a fake clock. Pass the clock in from outside. - Forgetting the zone. A fixed clock needs a time zone.
LocalDate.now(clock)uses that zone to work out the date, so the same instant can fall on different dates in different zones.
Exercise
Using a fixed clock set to 2024-03-01T08:00:00Z in UTC, what does LocalDate.now(clock) return, and does new Invoice(LocalDate.of(2024, 3, 1), clock).isOverdue() return true or false?
Show answer
It returns 2024-03-01. isOverdue() returns false, because isAfter is false when the two dates are equal. The invoice is due today but is not late yet.
Exercise
Write one line that creates a clock running two hours ahead of the real UTC clock.
Show answer
Clock ahead = Clock.offset(Clock.systemUTC(), java.time.Duration.ofHours(2));