Ayatullah Abdulhakim
Ayatullah Abdulhakim

Reputation: 43

Fixing Branch Coverage Sonar issue

I am trying to run sonar and i take a sample of my code a sampleClass to fix the branch coverage issue:

The issue was 117 more branches need to be covered by unit tests to reach the minimum threshold of 65.0% branch coverage.

I was trying to let my test cases cover many branches in the sample class.

but the number 117 cannot be changed after many trials. What i have to do to fix this issue?

Upvotes: 0

Views: 10102

Answers (1)

Stultuske
Stultuske

Reputation: 9437

You need to add more tests. For instance:

@Test
public void testThis(){
  if ( getBooleanA() || getBooleanB()){
    assertTrue(getBooleanA() != getBooleanB());
    }
  else{
    assertTrue(getBooleanA() == getBooleanB());
  }
}

here, you need to provide tests for the next cases: 1. boolean A and B are both false 2. boolean A and B are both true 3. boolean A is true, and boolean B is false 4. boolean A is false, and boolean B is true

if you miss one of those tests, there's a branch you haven't covered.

EDIT: it is clear (or it should be), that the assert in the else block is pointless, but I just added it, in case it didn't return a boolean, but an int, to show how easily it is to have a new branch that needs covering.

Upvotes: 4

Related Questions