1

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?

cee
  • 11
  • 1
  • 2
    Firstly, I'd try to avoid giving a class the same name as a class in `java.util`. Next, we can't tell how you'd do this without seeing what operations your class actually supports. Does it have an "add day" operation? Is it immutable (which is a good idea)? We need more context in order to help you. – Jon Skeet Feb 26 '15 at 11:37

2 Answers2

2

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.

Luke SpringWalker
  • 1,600
  • 3
  • 19
  • 33
1

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"
k3b
  • 14,517
  • 7
  • 53
  • 85