Evolving Software

I realize that I am not the first to write about unit testing, but there seems to to be an absence of examples dealing with only simple unit testing. Most all documents deal with unit testing as part of an overall strategy like XP or agile, I don’t really care when you do it as long as you do.

As you can see all we are doing is invoking a method on the class and asserting that it responds in an appropriate way, in this case when I set a string that should be available via the get method. All tests are of the format ‘public void test
() throws Exception { }’, and junit will be able to locate this test method and run it. But this test isn’t very comprehensive because it only executes 10 out ot the possible 45 instructions in our class, so we will need to do more testing.

Now we have a test that covers 100% of the instructions in the our class. Now I know that this is a trivial example but unit testing you code shouldn’t be much harder than this, if it is then it is probably a good sign that the complexity of your class has grown beyond what it should be.