The coverage of the lines of your Java assert should simply be covered by running your test suite with statements included, that is, providing -ea as a jvm argument. If you do this, you will see that cobertura easily reports 100% of the line if the rest of your lines are also covered.
However, assert lines will still be colored red, indicating low light. This is because your statements are usually always true, so you never end up in a false branch.
Thus, branch coverage in Cobertura is corrupted using assert , because assert lines will have 50% branch coverage, making the total percentage of branch coverage difficult to interpret (or useless).
Clover has the nice ability to ignore assert when calculating coverage. I have not seen this feature in any open source Java coverage tool.
If you use design -style statements behind a contract , there is no need to add tests that make your Java assert fail. As a matter of fact, for many statements (for example, invariants, post-conditions) you cannot even create objects that could lead to their failure, therefore such tests cannot be written. However, you can use invariants / postconditions to get test cases that implement their boundaries - see Figure Robert Binder invariant boundaries . But this will not lead to the failure of your statements.
Only if you have a very complex precondition for this method, you may want to write a test aimed at making the precondition unsuccessful. But then rethinking your precondition may be a better idea.
avandeursen
source share