06 April 2012

TDD benefits example

Hello everyone,
this is my first blog ever about test driven development (TDD).
The goal of this post is to show one of the many benefits of developing code in this direction, which means: unit tests first, implementation later.
Basically you should develop your most basic implementation first (maybe a class and a single method). Then you go and write a simple unit test for that method. At first run unit test will fail. Then you steadily increase the complexity of your implementation to achieve what is needed by your unit test. Finally when you do that, you can again increase the complexity of your test, by adding more test methods to test even more implementation code functionality. As you do that, some of the tests will fail again. Then you should keep refactoring the implementation code to make the tests green and so on.. That is the basic overview of how TDD should work.
The example i am about to show you is a simple piece of code written in Java, and unit testing framework is junit. So here we go.