Test Coverage:
I have 100% test coverage for my code base. I believed that, it is sufficient enough to deploy my code in Production and it is trust worthy that there won't be any bugs.
Issue:
But I had a very critical bug reported by my customer, which happened because of the code below:
Id tmpId;
if(account.Id == account1.Id || accountMap.get(account.Id) != null) {
tmpId = accountId;
}
- I always had the condition
account.Id == account1.Id
satisfied, so it didn't execute theaccountMap.get(account.Id) != null
statement anytime - But still since either one of condition is satisfied the code is assumed to be covered and it gives 100% code coverage
Real Usage:
- When there was a real condition with
account.Id != account1.Id
the code raisedunable to de-reference null
exception - This happened because of the case that
accountMap
didn't contain the key andMap#get(key)
method will raise an exception - I have replaced the
Map#get(key)
withMap#containsKey(key)
to correct this (which will returnboolean
instead of raising exception on absence of key)
Question:
- This bug escaped my eyes and even went into production because of the blind faith on Code Coverage
- So my question is how to cover
OR
conditions effectively so that both LHS and RHS of OR condition will be covered ? - If we can't completely rely on Code Coverage what is the better practice to cover this kind of bugs in an automated fashion ?