So Java 8 brought us ZonedDateTime and LocalDateTime. They seem to be a big improvement of the much maligned java.util.Date. However, I’ve got a couple of gripes with it.
Not long ago, I was writing a unit test (I’m a big believer in TDD). I use mockito a lot, and as I needed a ZonedDateTime, I decided to @Mock it. Thats when I found out I couldn’t.
What? Really? In this day and age?
Yes – its a final class. Those of you already familiar with mockito will appreciate the pain.
My question is this: Is there a good reason why its been declared as final?
If you don’t TDD in Java, I expect that you may think that the final keyword is a good idea, and you probably sprinkle it throughout your code. I used to do this a lot – particularly when declaring arguments for methods. I don’t care for it at all now – and I go out my way to remove them when possible.
So, I’m sure this is the reason that ZonedDateTime has been declared final – but I don’t know for sure. Any suggestions?
Mockito cannot mock/spy following: - final classes - anonymous classes - primitive types
NB: If you’re looking to find my other gripe (with LocalDateTime), then you need to know that I’ve extracted it out into my next blog! “Extract until you drop!”.