In javascript for example the jest library has a facility to kind of travel through time to test time dependent code, here, or in ruby on rails you have facilities to mock time like this. I'm looking for something similar in Scala that could allow me to mock calls to LocalDateTime.now
from java.time
for example, but I haven't found anything like the tools I just mention or how to mock LocalDateTime.now
Asked
Active
Viewed 29 times
0

Miguel Salas
- 692
- 1
- 6
- 21
-
@GaëlJ you didn't point to any particular answer – Miguel Salas Aug 24 '23 at 18:08
-
1By the way, I cannot imagine a scenario where calling `LocalDateTime.now` is the right thing to do. When capturing the current moment, you need the context of an offset-from-UTC or a time zone. That means calling `now` on `Instant`, `OffsetDateTime`, or `ZonedDateTime`. – Basil Bourque Aug 24 '23 at 19:21
-
1You shouldn't be mocking those classes. If you want to control the values generated, you should use a `Clock` and inject one which you control during your tests, and use `LocalDateTime.now(clock)` instead of `LocalDateTime.now()`. – Mark Rotteveel Aug 25 '23 at 09:17
-
I end up passing lambda to obtain the time that I mocked on the tests – Miguel Salas Aug 26 '23 at 15:00