Sunday, February 21, 2010

TDD - what should go first


Plenty of time has passed since the last time I opened The Blog.

The topic of the day is 'how you write code good'. I don't remember times when I had enough time for the unittests. So, i'm trying to figure out several rules for myself to stay when working. Now the following sequence seems to be doing its job:

How this happens now. I want to write a new business-logic method:
  1. I create a method stub in a (bean). With the name only.
  2. I think of what should this method get as input, and what should produce in output, in both normal and exceptional cases.
  3. I modify method to the correct signature.
  4. Then - i generate javadoc ;). In the javadoc I write down all the thoughts I had when was doint 2. of this list.
  5. I go to test. And create 1 test class for this method. In this class, I do write one-by-one all the cases (negative and positive) of how it's done.
  6. Mocking - I'd use it, but yet we have a pretty well-done structure, so I can rely on live objects. Though, I'd try using mockito for the brand new things.
  7. I run tests - they fail. That's good :)
  8. I implement method - tetsts should start working. If one of them fails, I need to correct either the tests themselves or the method - depending on what's error.
/me knows that all the stuff above is well-known, wide-used and so on. The list should be treated as a reminder for myself.

1 comment:

Dmytrii Nagirniak said...

I personally try to follow something like this (from top of my head):

1. Write what I need to do on a paper if I feel like.

2. Write high level tests with no any implementation and using non-existing helper methods for testing (so the tests will look like a custom DSL).

3. Write the helpers.

4. Start writing the implementation (at this stage I might get an idea whether it is one method or a number of different classes).

5. If necessary, write more low level tests.

6. Implementation.

7. Red? Go to 6. Green? Go to 8.

8. Refactor implementation and tests.

9. If feature is not finished, go to 1.