-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make Clock.System() replaceable / mockable #33
Comments
Some similar behavior is present in some of Kotlin's libraries. Fuel, for example enables changing the base instance Kotest supports simulating the current time through |
Python also has a way to simulate date and time for testing without having to inject a dummy instance. The same is possible with javascript and typescript, also discussed in this jestjs/jest#2234 |
@JakeWharton |
I thought is was a bad idea for coroutines Dispatchers and for Rx Schedulers and for arch components executors (which I thankfully don't use) and for every other static setter I've ever used in the history of programming. This is one of those decisions where people decry things like boilerplate and then spend the next five years complaining about test setup and non-determinism. We have a mechanism to make this straightforward and deterministic and it's inversion of control. Just supply the clock like you supply everything else that's a dependency. Nothing about a clock is special here, so why treat it as such? I don't want to have to ban the static clock with its mutable backing implementation the same way I do with Dispatchers and Schedulers. Just pay the parameter cost, it's cheap! |
Why other programming languages and frameworks implemented date and time this way and people always ask for date mocking specifically? |
JodaTime has similar functionality ( If you want to control the value of "now", then use dependency injection and inject a 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 |
Instead of injecting a
Clock
in every single function or class that uses the usual approach ofLocalDateTime.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
The text was updated successfully, but these errors were encountered: