I have been reading about TDD recently. Just curious to know if anybody has used Test Driven Development approach in their J2EE projects. What is your experience? My first impression about TDD is that its a great approach and will lead to a better design.
TDD *can* (not necessarily does) lead to a very clean design full of small objects doing one thing and one thing well.
Coupled with an IoC (www.springframework.org) it can be a killer :)
Also check out mock objects (jmock.org is the one I use).
You might also want to look into a continuous integration server as well (again, cactus is the one I use).
You *do* need to be careful about what you are testing. It is all too easy to test a whole graph of objects. Ideally you want to test a single class and use mock objects for it's collaborators.
J2EE does pose a horrendous problem because of it's use of container managed services. How do you unit test a sessionBean? Quite simply you cannot test it in isolation, you must test it in an application context. There are mock container implementations around (checkout junit.org).
One strategy I use is to move all my logic into Commands or helper classes which can be unit tested very easily.
Hi Unmesh, I've successfully used TDD in Enterprise Java applications. If you are interested in learning TDD, you can read this quick start article describing TDD with samples in Java, JUnit and JMock. Colin, I agree with you in general. TDD can be a blessing or a curse depending on your skills :) TDD and unit testing at all coupled with an IoC is my favorite style of coding in Java. It makes a code be stable, easy to test and maintain. It works because everything can be mocked so tests become really modular. I suggest to take a look at Spring for that purpose. With regard to continuous integration I would say that FIT testing or Concordion are useful frameworks in general. Truly speaking, I prefer to have thin controllers in Web applications rather than cover it with Cactus based tests. In my opinion, there is only one problem preventing developers to use TDD. It is inertness of our mind.