This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Improving code coverage" Watch "Improving code coverage" New topic
Author

Improving code coverage

Omkar Shetkar
Ranch Hand

Joined: Jun 22, 2006
Posts: 41

Hi,

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


http://writingsontech.blogspot.in/
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

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


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, 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.
 
jQuery in Action, 2nd edition
 
subject: Improving code coverage
 
Similar Threads
JUNIT how many hours need to learn it ?
0% code coverage using Clover!
Various Testing Frameworks in Rails
RAD code coverage 'per-line coverage highlighting'
EMMA and J2EE