Recently I read article about testing time in Java (written in Polish):
I think that, it’s worth to prefer usage of classes provided by JDK, than creating special interfaces. I would like to present you, my approach for testing time in Java.
Let’s have simple entity with 2 LocalDateTime fields.
When we create our entity, we initialize expiration date with one month period.
Entity has also simple update method to set updatedAt field.
Ok, let’s test time!
If you prefer injecting values, your entity will look like this:
Now test would look like:
I think, that this is a simple way to test time operations in our classes without any specific classes, using only JDK.
Hope you enjoy this post. If You have any questions or problems leave a comment or send email.
See You soon!