Saturday, September 5, 2009

Test Driven Development (TDD)

Been reading a bit on TDD recently and for some reason it has caught my attention. This is not something i have done ever in my kind of short carrier in IT :). But its a new concept for me and i kind of like how it looks at software developement. Hence it got my attetion.

What TDD specifies is you write your unit test class first. You define the classes you may need. Of course at first your test will throwa Class not found exception when you run the test. But this is what TDD is all about. You write the minimal code required to get the test parsing. So what you do first afte running the test is you go create the implimentation class so that the test will compile and pass. And so and so forth you add functionality little by little and improve your design as you go foward. You always only implement the functionality you need to pass your test so it will help you save time and get a much cleaner code because you will be refactoring your code as you do your development and test it as well in the process. Not only will you end up with a nice looking code base, but it will be a well tested code base with much more code coverage.

Ofcourse we know that most of the times we are dealing with database code within our codebase. But not to worry. EasyMock to the rescue. This framwork (http://easymock.org/) is able to mimick your database depenedent code, more specifically you DAO classes and is able to mock it so you do not need to create database connections within your test classes(which is a bad practice by it sef becaus then you will leave your database in an inconsistent state after running your tests). But one change or more over if your developing a component from scratch to note is you need to implement a setter method or constructor to swap in your DAO implementations at runtime so that we can swap in our mock implementation as we run the test to tell the code base to use our mock implementation at the time of running the JUnit tests. More info on how to configure EasyMock can be found in the below locations;

http://www.ibm.com/developerworks/java/library/j-easymock.html
http://www.michaelminella.com/testing/unit-testing-with-junit-and-easymock.html
http://www.realsolve.co.uk/site/tech/easymock.php


TDD is pretty cool IMHO. Im gonna be trying it in my future development and see how it scales. If any of your are already using this methedlogy please share your comments. Would like to know all your views.

Until my next post its adios from me again!!!!

Cheers

2 comments:

  1. It really helped me a lot..initially you will think that you are wasting your time but later you reliazed that it's time saving and less bug free code.

    ReplyDelete
  2. very true Dipak... I understand this now that i have started practicing the same.. Really helps to refine my code.. Thx for sharing your thoughts

    ReplyDelete