javacode-coveragejacocoemma

Getting Java coverage from jar execution


I have a JAVA application without unit tests (I know, inherited code), and I need to get the coverage from several tests which are defined as executing the jar file with some presets.

I used to do it with Emma from console like this:

java -cp emma.jar emmarun -raw -ix +some.filter.* -out ./coverage/test001.em -jar MyJarFile.jar <application arguments>

The problem is the filtering with -ix argument. I wanted to get coverage only from my own packages, which are named com.mycompany.whatever.blah.blah.blah. Therefore, by inclussion filter is supposed to be just +com.mycompanyname.*. Nevertheless, I only get about half of the packages, and not even every class into each package.

I also tried other combinations, such as using only exclussion patterns to avoid all the non desirable packages, and the result is the same. I known I manages to do it somehow in the past, but I am afraid I am unable to remember how.

So that is why I am looking for a different coverage library which allows me to get coverage from jar files. I saw JMockit, Jacoco and even the impressing Sonar, but it seems that they don't allow jar-coverage and that they are designed for unit testing coverage.

Any suggestion?


Solution

  • JaCoCo includes an agent called jacocoagent.jar which does the job. I could not find examples online, but I tried following the documentation and this works:

    java -javaagent:./route/to/jacocoagent.jar=destfile=./coverage/test001.exec,includes=com.mycompany.* -jar MyJarFile.jar <Jar arguments>
    

    Then I can load the exec files into Eclipse with the eclemma pluggin, merge them, and export as a HTML file. I think it is possible to generate a HTML report from ant/maven or even with a Java application written for that purpose (jacoco developers provide an example), but I haven't tested those options yet.