Skip to content

Instantly share code, notes, and snippets.

@virtix
Created October 25, 2011 11:13
Show Gist options
  • Save virtix/1312340 to your computer and use it in GitHub Desktop.
Save virtix/1312340 to your computer and use it in GitHub Desktop.
Shows how statement coverage can be higher than branch coverage. This is an inverse subsumption relation.
package test;
import static org.junit.Assert.*;
import org.junit.Test;
import junit.framework.TestCase;
public class CoverageTest {
/**
*
* Computes 100% statement coverage but 75% branch coverage.
* */
@Test
public void testExists(){
int[] a = {0,-1,1,100,99};
int x = 99;
assertTrue( exists(x, a) );
}
/**
*
* Our code under test.
* */
public boolean exists(int x, int[] a){
boolean ret = false;
for(int i=0; i < a.length; i++){
if( x == a[i] ) {
ret = true;
break;
}
}
return ret;
}
}
@virtix
Copy link
Author

virtix commented Oct 25, 2011

Clover computes this as 100% statement coverage and 75% branch coverage. But Clover doesn't show "which" branch is not covered. I suspect it's the i < a.length condition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment