question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make Clock.System() replaceable / mockable

See original GitHub issue

Instead 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:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
kluevercommented, Aug 14, 2020

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.

1reaction
dkhalanskyjbcommented, Jun 1, 2021

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found