Blog
Deterministic Time Tests: Stop Sleeping And Inject A Clock
Thread.sleep(1000) does not prove a timeout. It proves the test machine waited roughly one second and you hope scheduling behaved.
Inject Wall Time
final class TokenPolicy {
private final Clock clock;
boolean expired(Instant expiresAt) {
return !clock.instant().isBefore(expiresAt);
}
}
Tests use Clock.fixed and choose instants exactly before, at, and after expiry. Production uses a system clock with an explicit zone.
Control Scheduled Work
For retry delay or scheduler logic, test the policy separately from real waiting. A fake scheduler or virtual-time facility can advance pending tasks deterministically.
One integration test may still verify real framework wiring, but hundreds of sleeps create slow flaky suite.
Test daylight-saving boundaries when local business time matters. A fixed UTC noon test cannot cover a missing local hour.
Keep monotonic duration separate from wall-clock time. Deadlines and elapsed-time measurements should not become negative when the system clock adjusts. The injected abstraction may therefore expose a wall clock for business timestamps and a monotonic ticker for measuring intervals.
If a timing test fails only on busy CI, remove the timing guess. The CI machine is revealing design, not misbehaving personally.