ad-hockery: /ad·hok'@r·ee/, n.
Gratuitous assumptions... which lead to the appearance of semi-intelligent behavior but are in fact entirely arbitrary. Jargon File

Articles tagged ‘jdk8’

Fixing current time for tests with Java 8's date/time API

For years I’ve used the Joda Time library to provide a nicer alternative to the horrible mutable java.util.Date class and the terrifying abyss of java.util.Calendar. One thing, as a fanatical tester, that really appealed to me was the existence of the DateTimeUtils class. All the Joda Time types use DateTimeUtils as a source of the current instant and it exposes methods that allow tests to fix or offset the value that’s returned. That can rule out some irritating flakiness in tests and enable testing of time zone / daylight savings bugs, timeout logic and so on while retaining the encapsulation of timestamp generation in production code.

Of course, when you look at DateTimeUtils with purist eyes it’s a horrible hack. A static singleton encapsulating global mutable state! I guess that was the attitude of those responsible for JSR-352 that created the new java.time package which is largely based on Joda Time. One of the things that wasn’t carried over from Joda Time is the DateTimeUtils class. Instead factory methods such as use a Clock object – by default Clock.systemUTC().

Read more…

Web Statistics