Strategies for Dealing with Date and Time in Test and Production Code
summary
Shows that time is a hidden dependency and replaces hacks like sleeps, reflection and manual clock changes with explicit java.time Clock injection, fixed clocks for unit tests and a mutable Clock bean for Spring integration tests, plus a TestExecutionListener to reset state. Demonstrates code on GitHub, uses Awaitility for external asynchronous waits, and enforces the rule with an ArchUnit test that fails builds when Instant.now/LocalDate.now are called directly, producing deterministic, fast tests.
AI-written summary · about
chapters (11)
- 0:00Introduction to talk
- 2:58Date creation in code
- 7:19Problems with system clock
- 13:05Introducing Clock dependency
- 18:41Production code with Clock
- 19:46Fast tests with mutable clock
- 26:17Slow tests with Spring
- 32:24Enforcing Clock usage
- 35:20Waiting for async operations
- 39:14ArchUnit demo and summary
- 41:08Final advice and Q&A








