javaconcurrencycode-coveragesynchronizedemma

Why doesn't EclEmma cover syncronized(MyClass.class)?


I'm using EclEmma for coverage analysis.

My Java code includes a synchronized(MyClass.class) {} block.

EclEmma says it is only partially covered, event though I've got a unit test in which one thread gets access and another thread is blocked.

Is it possible to get full coverage of synchronized using EclEmma?

Can I annotate the code in some way to tell EclEmma to give this line full coverage?

Kind regards Roger


Solution

  • I am not sure it is possible to get a full coverage, since issue 2939804 reports:

    EMMA always marks synchronized(..) as partially covered

    Examples:

    synchronized (lock) // partially covered (yellow line in EclEmma)
    {
    // ...
    }
    synchronized (this) // partially covered (yellow line in EclEmma)
    {
    // ...
    }
    

    Maybe a different tool (like Cobertura) would yield a different result? (I have not tested it recently).


    Update December 2012 (more than 2 years later):

    Nathan D Ryan reports:

    synchronized will light to green if the synchronized block contains code that waits on an object monitor, and a test interrupts the waiting thread.

    After a little experimentation, I was able to achieve complete coverage of the synchronized line if the synchronized block completed normally and completed abruptly due to an exception.