0

We have put build breakers in our CI process for failing the build for insufficient junit coverage. The problem is with how to increase the code coverage because earlier we had broken down the methods in to smaller private methods due to sonar complexity issue.Now we need to work our way to add more junits to test each of the methods.

what are the best options?

1) Change the access modifier to protected or default? I have already seen the question of how to test private method. 2) Use reflection? 3) how best to work towards increasing branch coverage?

Example:

    Class A {

    public Object complexLogicMethodA(){

      //1.Call private method B
        methodB();

    //2.Call private method C
        methodC();

    //3.Call private method D
        methodD();

    //Based on 
    }

    private methodD(){
     if(){
     //some code here
     }else{
     //Some code here
     }

 }
}
learnerFromHell
  • 183
  • 1
  • 9
  • 3
    Before you resort to either changing access modifiers or reflection, note that you should be able to get high coverage of your example code just by thoroughly testing `complexLogicMethodA()`. – Bill the Lizard Feb 23 '16 at 17:47
  • Just call `complexLogicMethodA`. You should not focus on testing privates methods. You must test the behavior of your class. All the stimuli of this class with her external API should trigger the private methods. And then, if you can't reach these methods, it's because of dead code – Jérémie B Feb 23 '16 at 20:55
  • So you mean I should add multiple tests which traverses each and every line of the internal private methods. I should be adding more test with different test data so that branch coverage is achieved. – learnerFromHell Feb 24 '16 at 03:18
  • Whether or not you should test your private methods is a matter of debate, some say yes, some say no. Generally a private method is simple enough to not require a test and sometimes is complex enough to warrant one. Again that last part will cause debate with some. Code coverage doesn't generally mean your code will work; I'd start by negotiating with the folks who are demanding X% coverage. In my experience those folks are not developers and just read an article and are trying to appear like they know what they are talking about. If they are set on X% coverage then I would use reflection – aemorales1 Feb 24 '16 at 14:37

0 Answers0