Why does Java Thread.sleep or Date.after () work differently on other computers?
I have a unit test that checks that an object inside my instance has been instantiated with an appropriate Date position:
Date earlier = new Date();
Thread.sleep(10);
instance.execute();
assertTrue(instance.getMyObject.getCreationDate().after(earlier));
This test works for me on Windows 7 Java 6 when running in Eclipse or using Ant from the command line. However, my client says that this test is failing for him (he did not specify his environment). How could this be?
+3
source to share
3 answers
As Peter Laurie replied, the system clock permission may mean it hasn't updated between your "earlier" date and instance date being created. Maybe change your code to the following:
assertFalse(instance.getMyObject.getCreationDate().before(earlier));
This way, you check that it is definitely not earlier than your earlier date, and you do not need to worry about whether the system clock is updated or not.
+1
source to share