• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Improving code coverage

 
Omkar Shetkar
Ranch Hand
Posts: 76
1
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34218
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Raja Pal
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic