Tuesday, June 02, 2009

Test Driven Development

So I posted an entry a little while ago... ok perhaps it was more of a rant about unit tests.

I don't disagree with the post, however I have learned a little bit more about testing.  Specifically what testing is and what it is not.  I recently was able to attend a JUnit class that was given by J.B. Rainsberger.  He also authored the JUnit Recipes book it really is a good book and worth the price.

Some of the things I have taken away from the class is that unit testing pretty much sucks.  However, behavior driven design is pretty cool.  Unit testing (IMO) is going through code (usually code that exists prior to the test being written) and writing a test to figure out how the code works.  This is a very effective way to trouble shoot existing code but is a very large effort, both in time and mental CPU cycles.

Behavior Driven Design is a technique that lets developers take an abstract concept (or a problem) and get it into code.  Testing frameworks like JUnit and JMock are helpful tools to do this with but don't get confused you aren't testing anything.  You can't test anything because the code doesn't exist yet - you are modeling the behavior you want your code to exhibit.  This is a powerful technique and I have done it many times on many projects but I never had a name for it.

I recently implemented a restful webservice using a new java framework called cxf.  I was itching to try out the techniques of Behavior Driven Design that J.B. showed us .  Unfortunately, I didn't have a good understanding of cxf so I was unable to use BDD effectively for quite a while.  I fell into the difficult place that java developers fall into of iterating over configuration changes and server restarts.  This is an important point not all of our efforts as developers can be boiled down into writing test cases or even BDD.  In fact, I did try to do some BDD work prior to getting the frameworks working but ended up throwing all the code away. The cxf framework changed how I thought the code would eventually work.

If anyone is interested I'll post some code examples of how the code ended up looking.  But the point I am trying to make is that doing Behavior Driven Design, although leaves a whole lot of unit tests lying around as artifacts of the process that is not the goal.  Unit testing on the other hand is an extreme effort that we do as either a last resort or as an ego trip to get our testing coverage up to 100%.

Don't confuse behavior modeling with unit testing - they are worlds apart.

 

-Aaron

No comments: