EMMA and TestNG for Simple Java Code Coverage

December 07, 2008 📬 Get My Weekly Newsletter

Although RCov makes code coverage in Ruby dead simple, I wasn't sure how easy this would be to achieve with Java. The first free tool I found is called EMMA and it was surprisingly easy to setup, especially since the documentation isn't geared toward getting coverage during tests (but getting it during execution).

EMMA works by instrumenting the classfiles to analyze coverage. Although it can do just-in-time instrumentation, that didn't seem to work for recording coverage via TestNG. The offline instrumentation makes is pretty easy to use with anything. Basically, you want your ANT file to:

  1. Compile your code
  2. Use EMMA to instrument your classes to a different directory
  3. Run your tests, using the instrumented classes first in your classpath
  4. and passing a few system properties to your running code
  5. Run EMMA's report generator on the output
At first, I was getting some runtime errors because interfaces are not instrumented (and don't show up in the location you tell EMMA to put them). The solution is to put both your instrumented classes directory and your regular, non-instrumented classes directory in the classpath, making sure the instrumented ones are first.

Here's my test.xml I'm using in my fork of ImportScrubber that shows it all working together. All in all, it only took about 15 minutes to set up and debug. Of course, now, the tests that came with ImportScrubber provide almost no coverage, but that's another story....