Biscuit128
Biscuit128

Reputation: 5408

How to Junit test and expect an exception whilst maintaining code coverage

I am trying to write some JUnits for my application, I believe I have got around 80% code coverage, but when i run the tests my report looks as follows;

code coverage

This is giving me a code coverage result of 52% which is not idea.

Is there an alternative way to write the methods in which an exception is expected so as to maintain my coverage score?

Thanks

Upvotes: 3

Views: 3284

Answers (2)

dkatzel
dkatzel

Reputation: 31658

Don't worry about code coverage of Tests only the code coverage of the source code (not test code).

If you are using emma or jaCoCo, you can use filters to only instrument or report the results of specific classes or directories.

This is much easier to do if you have separate folders for src and test

Upvotes: 2

Paweł Adamski
Paweł Adamski

Reputation: 3415

Probably you are using Eclipse EMMA plugin to measure code coverage. As described here http://emma.sourceforge.net/faq.html#q.blockcoverage Emma measures block (not line) coverage, so when your code throws exception at some point that block is not covered because end of it was not reached. EMMA FAQ says:

block is a sequence of bytecode instructions without any jumps or jump targets. In other words, a basic block always (in the absense of exceptions) executes as one atomic unit (if it is entered at all)

and

A covered basic block is thus guaranteed to have executed without failures at least once in a given coverage run session.



One more thing.
You shouldn't look on coverage of your test classes, focus on business (regular) one.

Upvotes: 0

Related Questions