Clock (java.time)
The java.time class that supplies the current time, which code can receive as a dependency so tests can control what now means.
In more detail
Production passes the system clock; a test passes Clock.fixed to choose an exact instant. Calling Instant.now(clock) instead of Instant.now() removes a hidden global and makes time-based rules deterministic.
Example
deadline.isBefore(Instant.now(clock))Related terms
- dependency injection — Giving an object the collaborators it needs from outside, usually through its constructor, instead of letting it create or look them up itself.
- deterministic — Always producing the same result from the same inputs, with nothing left to chance or timing.
- test double — Any object that replaces a real dependency in a test: a stub returns canned data, a fake is a small working implementation, and a mock checks interactions.
Taught in
- Designing Testable Code — Modern Java: Virtual Threads & High-Throughput Services
- Dependency Injection And The IoC Container — Modern Java: Virtual Threads & High-Throughput Services