Make Clock.System() replaceable / mockable
See original GitHub issueInstead of injecting a Clock
in every single function or class that uses the usual approach of LocalDateTime.now()
, consider adding a way to replace the System clock instance if one wants to.
This would make classes that depend on the abstract context of now easily testable, and idiomatic, as we wouldn’t need to inject it everywhere just for the sake of testing.
I’m happy to provide a pull request for this possibility
Issue Analytics
- State:
- Created 3 years ago
- Reactions:10
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Mocking time in Java 8's java.time API - datetime
The closest thing is the Clock object. You can create a Clock object using any time you want (or from the System current...
Read more >Overriding System Time for Testing in Java
The now() method of this class allows us to get the current instant from the system clock in the UTC timezone. Let's see...
Read more >Mock Around the Clock - Java Code Geeks - 2022
It's very convenient in production code to create a variable containing something like DateTime.now() and then perform time calculations between ...
Read more >Java & SpringBoot: Overriding system time for Unit, Integration ...
It's to replace all of the getting time invocations (like LocalDateTime.now(), new Date() … etc) by invoking a custom DateTime provider. It's very...
Read more >Second Chance (or Clock) Page Replacement Policy
It can be implemented by adding a “second chance” bit to each memory frame-every time the frame is considered (due to a reference...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
JodaTime has similar functionality (
DateTimeUtils.setCurrentMillisFixed(long)
and friends) and it’s been a nightmare in large code systems. If some how one of your dependencies manages to use this in production, you’re going to have a really bad time. We went as far as using Error Prone to prevent non-test code from using these APIs.If you want to control the value of “now”, then use dependency injection and inject a
Clock
into your system under test.Static mutable state is almost always a bad idea.
Looks like the consensus is against the static setter idea, so closing the issue. The lesson from this discussion for us, the library writers, is that people do want to test their code using a mocked
Clock.System
. In practice, this means that we should strive to keep the surface area of the system-dependent subset of our API small, attempting to present as much functionality as is reasonable using pure calculations that never need to be mocked. Thank you all for the discussion!