javamockingalarmdailybuildsroutines

How to use mock object mimicing a daily routine program?


My program has a daily routine, similar to an alarm clock event. Say, when it's 2pm(The time is the system time in my pc), do something for me.

What I want to do is to speed up the testing period(I don't really want to wait 4 days looking at the daily routine and check errors.) I read on wiki of Mock object, the writer DID mention alarm clock program. I was so happy to see but still, don't know how to do it.

I am new to Mock Object, and I am programming in Java. So JMock or EasyMock(or any similar) might okay to me.

Thanks


Solution

  • Whenever you need to get the current time, don't use the system clock directly - use an interface such as:

    public interface Clock
    {
        long currentMillis();
    }
    

    You can then implement this with a system clock for production, and pass in a fake for tests, where you can set the fake to whatever time you want.

    However, you'll also need to mock out whatever's driving your system - are you explicitly waiting for a particular time, or does something else call your code?