I am writing an app at the moment that has some pretty extensive business logic based around dates. I have several hundred thousand records to test and testers who want to see how those records are handled, which has worked well so far, however, some edge cases are difficult to replicate. The reason is that most of the logic is related to today's date, in one way or another. What is the best way to handle this with both unit testing and for traditional testing? The only way I can really think of is allowing for today's date to be faked and fixed.


You can use mocks (dependency injection) to return "today" date to be whatever you want it to be. This will allow you to test problem dates and make sure that new additions will not break the old code.

There are plenty of mocking framework around and I am sure $language that you are using has at least a couple of good ones.

| improve this answer | |

Faking and fixing the date are the right way to do it. It can actually morph into a useful feature -- how many times have you wanted to be able to run a piece of logic using past data?

| improve this answer | |

By far the simplest thing is to intercept the function calls that return the current date, and when testing, always return the same date. The technique for doing this will vary depending on your implementation language.

| improve this answer | |

Also depends on what you are testing for. I created a database using JPA once and used the JUnit package. I had "From" and "To" date fields to process and the fields were upto miliseconds as there were some fast sampling involved. What I can recommend testing initially for any date-related things are:

1) Timestamp format (e.g. dd-MM-yyyy HH:MM:SS.sss) validity 2) Timestamp sanity (does "Today" return what the actual i.e. computer date is?)

java.util.date package is a primitive package and I expect them to be tested well in order to cover further sanity checks. The other things I will still keep in mind whilst testing are:

1) Parsing 2) Timezone 3) Validating user Input (if you are accepting dates as user inputs). 4) Type mismatch (are you going to keep numeric or string dates?)

If you are using C++, Boost, QT , may have some ready tests for these. Also check UnitTest++, xUnit to see what they offer in this area.

These are the thing I used for testing and worked fine for me :)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.