I'm still getting my head around parts of TDD. I've got a new library I'm writing so this seems like a good opportunity to try it out.
What I've read on TDD advertises 100% code coverage, but that seems a bit ivory tower, so I configured JaCoco to require 90% code coverage to give me some breathing room.
I started out on the code that loads the KeyStore. There's lots of boiler plate code and lots of checked exceptions. So starting here makes my life easier down the road. Everything looks good, and my tests are passing. But code coverage is only at 49%. Looking through the code, everything is covered except for what I'd call "Impossible Exceptions" like this one:
public void saveKey(Key key, String alias) {
KeyStore.SecretKeyEntry entry = new KeyStore.SecretKeyEntry(new SecretKeySpec(key.getMaterial(), "AES"));
try {
keyStore.setEntry(alias, entry, new KeyStore.PasswordProtection(password));
} catch (KeyStoreException e) {
throw new UnexpectedErrorException("Failed to save the key", e);
}
}
In this particular case, according to the docs, KeyStoreException is thrown if the keyStore hasn't been initialized. I'm coding defensively and have guaranteed that the keyStore will be initialized at this point. So the KeyStoreException cannot be thrown. But it's a checked exception so I have to deal with it, so I'm wrapping it in a custom RuntimeException.
Problem is I have no way to trigger this error in my unit tests. In fact I've done everything I can to make sure that it won't happen.
Given cases like this how does TDD achieve a mythical 100% coverage?
I could mock KeyStore, but advice from Mockito is "Don't mock types you don't own." So I'd rather not. Also KeyStore relies a couple static methods where Mockito doesn't help, and I don't want to bring in PowerMock for a simple case, and I'm not that convinced that throwing more libraries at the problem is an ideal solution.
So:
- Is TDD's 100% code coverage mythical?
- Is there a technique to get code analysis to recognize this code as covered?
My anticipated solution for now is to move my configured 90% code coverage restriction down to 40 or 50 percent until I have more classes to bring my over-all average coverage up. But before I do that is there something I'm missing?