Ravi had blogged about the difficulties of getting people to write unit tests. I could relate to him since I have come across this problem quite often.
IMHO a developer gets turned off from writing test cases because
- Most developers dont have a clue on how to write proper unit tests. Most end up thinking integration test cases instead of unit testing.
- Proper unit testing (not integration testing) is hard work and needs proper design
- Estimating for unit test cases are not done or is under-estimated since we tend to have close to 2X lines of test code for a code of size X lines
A unit test in my definition should
- Should test only one class - Even if a method in the class under test, calls methods on other dependent classes, this test should be responsible only for verifying that this method works fine provided the dependent classes return correct values.
- Continuing from 1, the dependent classes should have its own tests to verify all possible code flows. Doing this from a higher layer increases the # of test cases you have to write.
- The unit tests for the higher layers (above DAO layer) should use Mocks (and ofcouse Dependency Injection). Use either jMock or EasyMock to mock out calls to other layers. If you are unit testing without Mocks it means you are doing integration testing between two classes since you verify functionality of both.
- Test boundary conditions like what happens if you pass in a null object, what happens if your dependent class throws an exception etc.
- Test that the class throws all exceptions declared in @throws (and any runtime exceptions) exactly under the conditions documented
- Test DAO's even if you are using ORM tools, by using an in-memory DB like Derby or HSQL
In addition to above a code coverage tool like EMMA or Clover is a must have tool to capture coverage and draw attention to lesser tested parts of the application. Configure this to generate a daily/weekly report or better still hook it upto your cruise control. In most cases the developer themselves take it up as a challenge to get the code coverage up.