Let's make 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 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% test coverage (because the x < 2 branch has been covered, as well as the other one). But we didn't find the bug, since using zero as value for x hides it (since 0 * something is always 0, y is irrelevant). We would have needed something like this...
assertEquals(12, doSomething( 1, 12) ); // fails, because it will be 1*1 == 1
The same problem can occur for any other situation, including a division by zero. Can't imagine a good example, but I guess you get the basic idea how having a 100% coverage does not mean finding 100% of all bugs. (A cool way to find them would be mutation testing, but that's a pretty advanced topic.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…