views:

46

answers:

2

Dear Sirs,

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

+1  A: 

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).

VonC
It seems like you are right. I tried this: Object synch = MyClass.class; synchronized (synch) {} but it didn't help, even though my test has one thread waiting and another thread getting the mutex.
Roger Wernersson
A: 

I believe the problem is MyClass.class which apparently is implemented using

http://emma.sourceforge.net/faq.html#q.fractional.examples

"Implicit branches due to a hidden Class.forName(). This case is rather unfortunate because it is pretty common and yet the programmer has almost no control over it."

"Because Class.forName() can throw checked exceptions, the compiler emits a catch block that rethrows them as unchecked. This catch block hardly ever executes in practice, but it succeeds in marking the line as partially covered."

I missed that on the first read-through.

I will try to re-write my code to get full coverage.

/Roger

Roger Wernersson