I would like to write a unittest to the function
import com.github.nscala_time.time.Imports.{DateTime, richReadableInstant}
def myFunction(ts: Long):Long = {
(new DateTime(ts) to DateTime.now()).toDurationMillis
}
Is it possible to somehow mock Datetime.now(), so that the test result does not depend on when the test is running?
From what I can see this library is a wrapper around
joda-time(which, as the official documentation recommends, should be dropped in favor ofjava.time, but I assume you have some constraint that forces you to work on a pre-Java 8 release).joda-timecomes with a collection of static helpers that, among other things, allow you to manage what the response is when a method asks for the "current time" (see their JavaDoc here).The easiest possible way (but possibly prone to mistakes due to the shared mutable state it relies on) would look like the following:
You can play around with this code here on Scastie.
As mentioned, this approach is a bit clunky and relies on shared mutable state, which makes it prone to confusing errors. You can build a nice small helper to make sure you can use a custom clock on a specific test and reset to the system clock once done. The required synchronization means a performance hit, but it's probably acceptable for your tests.
You can play around with this other example here on this other worksheet on Scastie.