Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 741
Roger C S Wernersson Avatar asked Sep 15 '10 06:09

Roger C S Wernersson


1 Answers

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.

like image 61
VonC Avatar answered Oct 01 '22 20:10

VonC