Thursday, May 25, 2006
TDD and Eclipse
During our daily work we try to develop our software following eXtreme Programming practices, and specially Test Driven Development (usually called TDD). I said "try", because sometimes it isn't so easy applying TDD in order to fix bugs or work with not-so-clean allready-written code. However, TDD works great for new classes, specially if you use Eclipse.
Lets give a silly sample: we need to develop a "Device" class, which should have an identifier, a description, a copy constructor and one method which sais us whether this device supports a specific audio format (basing on UAProf or WURLF data base).
We can approach this problem from three different ways:
Let's start coding the typical template code for one test case with JUnit, and we'll see that Eclipse will mark a lot of compilation errors. As we can see in right image, it says us "Device" class hasn't been found (it's natural, because this class doesn't exists yet). Double clicking over this red error mark, let's see a list with all possible solutions for this error, and among them: create new Device class. So we let Eclipse doing its work and create "Device" class for us, and we can continue with our tests.
After coding two test methods, we're in the same problem: we've several compilation errors, because we're calling methods which don't exists yet.
For instance, we've a test that checks our copy-constructor, and this constructor doesn't exists yet (neither all setters), so we click over error mark at left margin, and choose "Create constructor Device(Device)" option. With the rest of the compilation errors we perform in a similar way, although there will be some situations in which you should complete or change data types for some parameters which Eclipse isn't capable to deduce.
This way, we can achieve Eclipse works for us generating all templates for our classes and methods, basing on all calls we made in the tests code. And this can save us a lot of time.
Once we have all templates written by Eclipse, we can continue coding all logic code in order to get all tests passing (I suppose Eclipse couldn't help us in these tasks :)
Lets give a silly sample: we need to develop a "Device" class, which should have an identifier, a description, a copy constructor and one method which sais us whether this device supports a specific audio format (basing on UAProf or WURLF data base).
We can approach this problem from three different ways:
- The traditional way, coding directly the class, using it wherever you want, and fixing errors as they are quicked on the uptake.
- Another way is coding the class, and then coding an unit test for that class. There's a lot of frameworks can help us to work with unit tests, although we're using JUnit and J2MEUnit.
- And finally, we can start from the end and apply TDD: coding first of all our tests and then corresponding logic code. Warn you there're several scenarios where it makes any sense applying TDD, but in others it can clear you mind at all. For instance, if you have a clear idea about the public interface of your class (because you have already developed something similar or because you have a clear, full detailed design), probably you don't need code your tests before the logic. However, if you don't have a clear idea about the class you're going to code, begin with tests, and you'll see the outcome is much better than you expected.
Let's start coding the typical template code for one test case with JUnit, and we'll see that Eclipse will mark a lot of compilation errors. As we can see in right image, it says us "Device" class hasn't been found (it's natural, because this class doesn't exists yet). Double clicking over this red error mark, let's see a list with all possible solutions for this error, and among them: create new Device class. So we let Eclipse doing its work and create "Device" class for us, and we can continue with our tests.
After coding two test methods, we're in the same problem: we've several compilation errors, because we're calling methods which don't exists yet.
For instance, we've a test that checks our copy-constructor, and this constructor doesn't exists yet (neither all setters), so we click over error mark at left margin, and choose "Create constructor Device(Device)" option. With the rest of the compilation errors we perform in a similar way, although there will be some situations in which you should complete or change data types for some parameters which Eclipse isn't capable to deduce.
This way, we can achieve Eclipse works for us generating all templates for our classes and methods, basing on all calls we made in the tests code. And this can save us a lot of time.
Once we have all templates written by Eclipse, we can continue coding all logic code in order to get all tests passing (I suppose Eclipse couldn't help us in these tasks :)