Menu

#55 Better Handling of Asserts

open
nobody
None
6
2008-05-07
2008-05-07
John Lewis
No

Ian Dickinson wrote:

Sorry if this has been asked before; I did look in the archives but couldn't find anything.

I'm getting low branch coverage on code that includes java asserts to encode invariants. For example:

public void setFoo( String foo ) {
assert foo != null;
this.foo = foo;
}

@Test
public void testSetFoo() {
new MockFoo.setFoo( "bar" );
}

@Test (expected=java.lang.AssertionError.class)
public void testSetFoo() {
new MockFoo().setFoo( null );
}

I can execute both tests successfully, but cobertura will tell me that line with the assert was executed twice but did not traverse all branches. I'm passing -ea to the JVM to enable the assertions, which I can see is working since the tests themselves all pass.

I'm using the cobertura 1.9 plugin for Maven2 on Java6 (I've tried both 6u6 and 6u4).

Any suggestions?

Thanks,
Ian

Jeffrey Bennett replied:

To get full coverage, you'd need to run 3 tests:
1.) Asserts off
2.) Asserts on, conditional true
3.) Asserts on, conditional false

You've done #2 and #3

To my knowledge, there's no good way of doing all 3 tests leading to a source of friction between Cobertura and asserts.

Ian replied:

OK, I understand. Would it be possible to have the branch coverage algorithm take this into account? I don't know if the VM args are captured in the .ser file, but if so it might be possible (I'm guessing here, I admit) for the branch coverage calculator to note that there are two possible worlds (your #1 vs #2 + #3), and depending which one is the "real" world adjust the statistics so that the code either tests one of the one possible branches, or two of the possible two.

And if that isn't impossible, can I make it a feature request please? :-)

Discussion

  • Alan Gutierrez

    Alan Gutierrez - 2008-06-03

    Logged In: YES
    user_id=2089590
    Originator: NO

    Please implement this feature. Ideally, Cobertura would not expect to see #3 even when asserts were on. An assert is a condition that should never be true. A very fruitless execerise to test the Java assert mechanism.

    Certainly, running Cobertura with asserts off should only test #1 and not expect #2 or #3.

    I would be so happy to see this implemented.

     
  • Nobody/Anonymous

    +1. At least provide an option to just automatically set assertion lines to green or exclude/ignore them. It's simply not practical to do 1+2+3 as described above.

     
  • Nobody/Anonymous

    Hi,
    Ditto - I'm using the Maven 2 plugin and would like to be able to disable assertions for the surefire run that cobertura uses. I checked the source and haven't found any obvious way... is there a known workaround (other than doing it manually) for this?
    Thanks!

     
  • Benjamin Croizet

    +1 I also have low branch coverage because of asserts
    This feature would be very interesting

     
  • Alex Anderson

    Alex Anderson - 2011-08-29

    +1 also keen to see this feature

     
  • Dmitry Katsubo

    Dmitry Katsubo - 2012-05-10

    +1
    Sometimes assertions are impossible to cover, as they are too deep in the code.