Wednesday, 15 September 2010

java - Setting time and date in JUnit test fixture -


Some tasks need to be done in June as part of the Java code.

  • Therefore, according to the requirement, I have to set today's date in java.

    The problem is that the system clock (which you new date () ) Gives time, does not give you the time needed for your test.

    You can avoid this problem by starting the Indirection level instead of your code, ask directly to the Java API, what is the current time, your code will give you the clock Time tells. In your actual program, your watch uses the Implementation System Clock, for your unit tests, you use a Fake Clock , which says that whenever you want it What do you call clock to use your code dependency injection You have to type the clock interface (or abstract base class) and its concrete implementations.

    For this approach you need to change your existing code, so it is easy to test, or write it in the first place so that it is easy to test. This is a trick of unit testing.

    I am working on some code at the time which is time sensitive my clock interface just

      public interface clock {long while meeting (); }    

  • No comments:

    Post a Comment