开发者

Cobertura, suppress lines never should come

开发者 https://www.devze.com 2023-03-05 17:32 出处:网络
I am using cobertura first time. Everything works fine but what I wonder is, there 开发者_JS百科are lines in my code, which should never be called like:

I am using cobertura first time. Everything works fine but what I wonder is, there 开发者_JS百科are lines in my code, which should never be called like:

try {
    em.persist();
}catch(Exception) {
    logger.error("can not create");
}

I am doing all controls until persist line, when my code works fine, error line never be reached and now it is so. Because of it I never have %100 line coverage.

Should I somehow suppress it?

Thanks, Bilal


While you can exclude entire classes from your Cobertura coverage reports, you cannot exclude specific lines or methods.

You should accept that 100% coverage is impossible to achieve in real-life projects, since there will always be unreachable code or code that can only be reached by simulating a complex combination of conditions.

As other answers suggest, you can try and increase the unit test coverage by mocking/stubbing an EntityManager to throw an exception on persist. But this is pretty much as far as you can go.


write an unit test which covers the exception throwing case.


I wouldn't say an exception occuring from the "persist" method can't happen. What happens if the db connection parameters are wrong, or someone mucked with the mapping annotations and messed up a column name? That would all trigger an exception. Catching and swallowing this can cause your unit test to succeed when it should fail on the exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号