I am trying a little TDD for the first time. I started with absolutely nothing and wrote some tests for a self written Date library.
I want to be able to do for example:
MyDate m = new MyDate(1,1,2012);
and assert that
m.equals(new MyDate(2,1,2012)) is false.
Now with only these two items, I could write a constructor that simply doesn’t do anything but still fulfills “being able to call the constructor with three ints to construct a date”. And I could write an equals method that always returns false.
So far, I haven’t written any attributes, although I’m preeeetty sure I want to have at least three of them, corresponding with the arguments of the constructor. Should I go on writing a test and then ‘discover’ these attributes as I go on? Or am I pushing it too hard and should I just write a basic class with the attributes I already know are needed?
if you want to learn TDD well, I suggest you to do it by the book. Write a little test, see it fail, make it pass, remove duplication, repeat. Even before this, you should write a little list of all the tests that you think you will need to write. This is the kind of “thinking ahead” that works well with TDD. You will come back to this list to cross off the tests that you made pass, and to add new tests that occur to you while you work.
I suggest you not to try variations on this process before you have learned it 🙂
Your little test is good enough; you must make it fail first by writing an equals method that returns true. Then you make it pass, then you write anther test that proves that two dates can be equal if they have the same attributes. It will take you only a few minutes.
Good luck!