Take another example ...
// The method, according to our imaginary specification, should do: // return x * y, IF x is less than 2 // return x + y, in any other case public int doSomething(int x, int y) { if (x < 2 ) { return x * x; // this is our bug, it SHOULD be x * y } return x + y; }
Now imagine that we have two tests:
assertEquals(0, doSomething( 0, 12) ); // works, as 0 * 0 == 0 * 12 assertEquals(20, doSomething( 10, 10 ) ); // works fine
So now we have 100% testing coverage (because the x <2 branch was covered, as well as the other). But we did not find an error, since using zero as the value for x hides it (since 0 * something is always 0, y does not matter). We would need something like this ...
assertEquals(12, doSomething( 1, 12) );
The same problem can arise for any other situation, including division by zero. I canβt imagine a good example, but I think you understand that using 100% coverage does not mean finding 100% of all errors. (A cool way to find them is through mutation testing, but that's a pretty complicated topic.)
source share