Test Program for Date class

416 Views Asked by At

I have written a Date class, which adds/subtracts days/months/years taking into consideration leap years. I now need to write a test program with hard coded data to test my implementation. The easiest way would be to create a "reference" date and then have a loop which takes it forward one day at a time, over say a 3 year period (including a leap year) and then print out the reference date together with some other dates constructed relatively from it. I haven't got a lot of java experience so I am wondering how I would actually do this?

2

There are 2 best solutions below

0
Luke SpringWalker On

You don't actually need to test all those cases. Think about testing cases that represents a set.

i.e. it has no point to test a given date and the day after, as internally would behave just the same (unless we are talking about February, that has an extra day every four years).

Try to design one test per case. I'd, for example, test a random date, a month with 31 days, another with 30, February as a special case, what would happend if a null is passed to the function.

This way, if something goes wrong, you will know easily what is going on.

0
k3b On

to loop through the dates by adding 1 day would work but in my opinion it is to complicated. Having a few examples would be enough:

(pseudo code) for leap year

// assuming 1996-02-28 is a leap year which i have not verified
MyDate feb28th = createDate("1996-02-28");
feb28th.addDays(1);
AssertEquals("1996-02-29", feb28th.toString());

(pseudo code) for non leap year

MyDate feb28th = createDate("1997-02-28");
feb28th.addDays(1);
AssertEquals("1997-03-01", feb28th.toString());

maybe you also need one test for every special cases like

  • "not every 400 years"