1

I need to set the system clock on certain date in my tests. This way I must simulate a behavior happening that date under a test. I found this article http://www.javapractices.com/topic/TopicAction.do?Id=234

But it does not give you much hint how to set the fake system clock e.g. 2.5.2013.

Please help me.

mjgirl
  • 1,214
  • 7
  • 24
  • 42
  • What is the challenge - setting the time, or working out what Unix time is equivalent to time and date in human readable format? – Rory Alsop Mar 04 '13 at 11:41
  • I think you should check this earlier [post](http://stackoverflow.com/questions/6203857/how-can-i-set-the-system-time-in-java). – Subhrajyoti Majumder Mar 04 '13 at 11:42
  • Setting the date so that the tests kind of "thinks" its running on certain date. – mjgirl Mar 04 '13 at 11:45
  • There's no way, in any system-independent fashion, to "fake" the system clock. It may be possible on some JVMs, but the technique would not be portable. You have to change the time as reported by the OS -- change your hardware clock. – Hot Licks Mar 04 '13 at 11:49
  • You might want to take a look at these questions: http://stackoverflow.com/questions/2001671/override-java-system-currenttimemillis and http://stackoverflow.com/questions/6203857/how-can-i-set-the-system-time-in-java . – Henrik Aasted Sørensen Mar 04 '13 at 11:50
  • Show us the code you want to test and what you've tried. Perhaps you should look into mocking frameworks?. – Anders R. Bystrup Mar 04 '13 at 11:50
  • Im not sure if the code gives any additional information? The scenario is that my application uses database and shows certain database data on certain dates. These information varitions should be tested with selenium tests. – mjgirl Mar 04 '13 at 11:55

3 Answers3

2

Assuming you need to manipulate time for your tests and not really change the system clock... consider using Joda-Time, this has the functionality baked in, e.g.:

// always return the same time when querying current time
DateTimeUtils.setCurrentMillisFixed(millis);

// then test 
assert new DateTime().getMillis() == millis;

// reset time
DateTimeUtils.setCurrentMillisSystem() 

You'll have to weigh up the trade off between using Joda-Time and implementing a solution yourself though!

Jonathan
  • 20,053
  • 6
  • 63
  • 70
0

In general is manipulating the system datetime isn't a good practice, i still remember each time when i ran the unittests how my outlook got crazy and how all kind of reminders from the past popped up.

You can create a static class which returns the datetime depending on the environment, so in the test Env. it will allow setting datetime which you can access while on real running it will return the system date/time.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
0

But it does not give you much hint how to set the fake system clock e.g. 2.5.2013.

It does. I suppose you followed the steps and replaced all the calls to system time with interface (TimeSource for example). If you need to simulate specific date you can use the following implementation in your test classes:

public final class TestTimeSrc implements TimeSource {

  public long currentTimeMillis() {
     Calendar calendar = Calendar.getInstance();
     calendar.set(2013, 4, 2);
     return calendar.getTime().getTime();
  }
} 
default locale
  • 13,035
  • 13
  • 56
  • 62
  • Maybe I'm missing something but does this really simulate the SYSTEM clock, not just returning certain dates in application? – mjgirl Mar 04 '13 at 12:08
  • @mjgirl That's the whole point. Create an intermediate class that will serve system time in production code. Then, when you need to test particular date values you can create fake intermediate class and don't bother simulating system clock. – default locale Mar 04 '13 at 12:13