I am doing unit testing for legacy code. When I have ran the code coverage tool, I see that very small part of code is covered by unit tests. I see that I have covered 100% for almost all public methods but code coverage is poor for other methods. What are the general guidelines that need to be followed to improve code coverage?
There are a few things you can do:
1) Look at the code to test more logic/paths through the private methods
2) Refactor the code to make it easier to test
3) Make some of the private methods package access to provide more entry points
If you have access to the legacy code(i.e. no decompilation restrictions) check to see if the public interface(class) can in various permissible(functionally) combinations generate better coverage results. If not check for data bounds check - very often the private methods act rarely, and most typically in these boundary conditions. If on the other hand these techniques don't work, just search by if-else, switch and ternary conditions(sometimes a very painful task, if code was written poorly and God classes / enthusiastic methods littter the code) and ensure you can manually create mocks which verify every line of behaviour/every flow path is visited.
Java Pal - Your friend in technology and innovation...India.