JaCoCo can't test certain uses of reflection? What are the workarounds?

1,831 views
Skip to first unread message

davi...@gmail.com

unread,
Jan 14, 2013, 10:32:26 PM1/14/13
to jac...@googlegroups.com

I just started using EclEmma ... thus JaCoCo ... for unit tests on a project that uses reflection, including getting the number of fields in a class, including static fields.  I was getting test failures and believe the cause is that JaCoCo apparently adds a static field (containing an array) to each class-undergoing-coverage (according to the "Control Flow Analysis for Java Methods" page, if I'm reading it right).
 
So, what are my workarounds?  (I'm running EclEmma through the Eclipse plugin.)
 
Can I: a) detect that my unit tests are running under EclEmma/JaCoCo so I can change their expected answers?  b) mark certain classes (which are strictly used as test examples, and which are typically nested classes - some static, some inner) as not-to-be-instrumented?  c) or ???
 
Thanks! -- David
 
P.S. I also can't figure out how to run, in Eclipse, an instrumented test in the debugger ... is there a way to do this?

davi...@gmail.com

unread,
Jan 14, 2013, 10:44:30 PM1/14/13
to jac...@googlegroups.com
Well, one possibility is the the "Coverage Runtime/excludes" field in the Java/Code Coverage preferences, which I found in a later post.  I would like to document here for posterity that that works for nested classes if you use a fully qualified $ name, e.g., "com.bakinsbits.utility.ClassInfoTest$X*" to exclude all nested classes in ClassInfoTest that start with the letter X.
 
I can get by with this, with a naming convention.  Although, the documentation says that even if excluded those classes will still show up as not covered, and it would be nice to be able to configure that.

Marc R. Hoffmann

unread,
Jan 15, 2013, 12:58:25 AM1/15/13
to jac...@googlegroups.com
Hi,

your observation is correct: JaCoCo instrumentation adds a static field
to every class holding a reference with the probes for this class. This
field is flagged as synthetic.

You reflection code should ignore synthetic fields. Also the Java
compiler adds such fields in certain constellations.

Best regards,
-marc

davi...@gmail.com

unread,
Jan 15, 2013, 8:57:06 AM1/15/13
to jac...@googlegroups.com
Marc, that's great! I've never heard of synthetic fields.  Thanks! -- David
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages