permaculture playing cards
The moose likes Testing and the fly likes Improving code coverage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Improving code coverage" Watch "Improving code coverage" New topic

Improving code coverage

Omkar Shetkar
Ranch Hand

Joined: Jun 22, 2006
Posts: 74


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?

Thanks in advance,
Omkar V S
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33130

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

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Raja Pal
Ranch Hand

Joined: Jul 12, 2004
Posts: 92
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.
I agree. Here's the link:
subject: Improving code coverage
jQuery in Action, 3rd edition