Java – cobertura override and assert keywords
The line coverage of the unit test I measured on cobertura is being affected because I have asserted that it is not covered in the test I should test assertions. Is there any way for cobertura to ignore them so that they will not affect my test coverage?
Solution
You should simply override the line override of Java assertion statements by running the assertion enabled test suite, that is, - EA as a JVM parameter
Nevertheless, the assertion line will appear in red, indicating insufficient coverage This is because your assertions are usually correct, so you will never encounter false branches
Therefore, the branch coverage in cobertura becomes chaotic by using assert, because the assertion line has 50% branch coverage, making the whole branch coverage percentage difficult to explain (or useless)
Clover has a good function to ignore assertions when calculating coverage I don't see this feature in any open source Java coverage tools
If you use assertions in the design - by - contract style, you do not need to add tests that fail Java assertions In fact, for many assertions (e.g., invariants, postconditions), you can't even create objects that will fail, so you can't write such tests However, what you can do is use invariants / postconditions to derive test cases that exercise their boundaries – see Robert binder's invariant boundaries pattern But this will not defeat your assertion
Only when you have a very tricky precondition for a given method may you need to consider writing a test designed to fail the precondition But rethinking your prerequisites may be a better idea