Menu

#61 Thrown Exception Causes Lost Coverage

open
CORE (51)
5
2006-03-11
2006-03-11
uburamba
No

Emma version: 2.0.4217
JDK version: 1.3.1_06

Here is an example:

public class SomeClass
{
public static void SomeMethod() throws
SomeException
{
SomeOtherMethod("Hello");
}

public static void SomeOtherMethod(String prm)
throw SomeException
{
... some code

throw new SomeException();
}
}

In this case the call to SomeClass.SomeMethod will
show up as not covered.

By adding a try/catch block the method becomes
covered.

public static void SomeMethod() throws
SomeException
{
try
{
SomeOtherMethod("Hello");
}
catch(SomeException e)
{
throw e;
}
}

Discussion

  • Jeanne Boyarsky

    Jeanne Boyarsky - 2006-08-07

    Logged In: YES
    user_id=1570433

    We are having this problem too. It works if the exception
    is thrown in the class under test, but not if it delegates
    that responsibility to another class.

    public void bad() throws Exception {

    try {
    etc.throwNullPointer();
    } catch (RuntimeException e) {
    ExceptionThrowingClass.throwRuntimeException();
    }
    }

    public void good() throws Exception {
    try {
    throw new NullPointerException();
    } catch (RuntimeException e) {
    throw new RuntimeException("system exception");
    }
    }

     
  • Thomas Lorenz

    Thomas Lorenz - 2007-02-01

    Logged In: YES
    user_id=648745
    Originator: NO

    I encountered this problem while refactoring JUnit Tests. Old code looked like this:

    @Test
    public void testSomething() {
    try {
    doSomethingNasty(); // Must throw
    fail("exception expected");
    } catch (SomethingNastyHappened e) {
    // good
    }
    }

    This structure made it impossible to achieve 100% coverage of the test code. So I refactored like this:

    @Test (expected = SomethingNastyHappened.class)
    public void testSomething() throws SomethingNastyHappened {
    doSomethingNasty(); // Must throw
    }

    Now this test method has exactly zero coverage - all lines are red...

     
  • Sascha Kiedrowski

    we're having the same problem over here ... will there be any solution anytime soon?

     

Log in to post a comment.