A part of our team’s “definition of done” is having unit-tests in place and, unofficially, a minimum of 80% code coverage. Our Maven-based build process runs the tests and then creates code coverage reports in HTML format, that we can then consult in the documentation that Maven generates. Along with other reports, this helps us get a clear picture of where our code is in terms of stability and quality.

This is all great ; but when you’re in the middle of a task and you write unit-tests, it’s quite tedious to run the build and then open the HTML coverage report just to monitor your code coverage. I spend about 80% of my time for a task in Eclipse : opening and activating tasks with Mylyn, coding, writing unit-tests, running unit-tests and so on. This is why for me it made sense to seek out a way to monitor the code coverage in Eclipse.

After a bit of googling, I found a Java code coverage plug-in for Eclipse : EclEmma. As you can see from its name, it’s based on the EMMA Java code coverage tool. Here’s a list with its main features :

  • a coverage mode in which applications launched or unit tests are instrumented and measured
  • coverage overview : a coverage view containing a report on the source code coverage values at project-level, package-level and class-level
  • source highlighting in the Java code editor using customizable colors
  • customizable coverage counters
  • multiple coverage sessions and session merging
  • importing EMMA coverage data files
  • exporting to EMMA coverage data, XML and HTML

The easiest way to install it is through the update site : http://update.eclemma.org. After the installation, you will notice a new launch mode appearing in the Eclipse toolbar, called Coverage, similar to the Run and Debug modes. This new mode allows you to run coverage reports on applications or unit-tests just like you would before run those applications or unit-tests.

In a project I’m currently working on, our server-side unit tests are written using TestNG. From Eclipse, I can run one or multiple TestNG units using the Eclipse TestNG plug-in, so I can easily verify that my code passes the unit tests. I have defined a launch configuration for each server-side project which runs all of the unit-tests. To check out the code coverage for those tests, all I have to do is create a coverage configuration and make sure that I select the source code folders to be instrumented.

I created a sample Java-Maven-TestNG project and added to it a simple class called ShoppingCartImpl along with a TestNG test class. Here’s how it looks :

Source code of a very basic shopping cart

Source code of a very basic shopping cart

As you can see, this is a very basic class. Now on to configuring the coverage settings ; this is a simple matter of clicking on the Coverage button in Eclipse’s toolbar and selecting the menu option Coverage Configurations…. This opens up the coverage configuration window, as seen below :

Creating a new EclEmma coverage configuration

Creating a new EclEmma coverage configuration

All I have to do is select the TestNG test suite that I want to run and check the source folders that are relevant to code coverage. I’m using Maven and all my source code is in src/main/java so I only select that folder. I click Apply and then I can finally run the coverage report. I click on the Coverage button and theTestNG configuration is executed and the coverage report is available :

Code coverage report after a first run

Code coverage report after a first run

In the bottom of the Eclipse window, you get a clear picture of the code coverage. As you can see, the are reports at project, package and class level, which also show up in the package explorer, in the left. To enable the decorators in the package explorer, go to the Eclipse menu and select Preferences -> General -> Appearance -> Label decorations, then make sure that the Java Code Coverage label decoration option is checked.

Another interesting feature of EclEmma is that after the code coverage instrumentation you can actually see the coverage in the source code. As you can see, each line relevant to the coverage report is marked with a color. Green is for 100% branch coverage, yellow is for some branch coverage and red for no coverage at all. The shopping cart has quite a low code coverage so I did shape it up. After a bit of fiddling with the code, I get a 100% code coverage and a very nice report :

The ShoppingCart class with 100% code coverage

The ShoppingCart class with 100% code coverage

Tagged with:
 

14 Responses to Java code coverage reports in Eclipse

  1. Cedric says:

    Nice article, I added a link to it to the TestNG documentation:

    http://testng.org/doc/misc.html

    Thanks!

  2. margelatu says:

    @Cedric
    I’m glad you liked it. Thanks for adding the link. And great job with TestNG, by the way.

    Cheers !

  3. DKD says:

    In the upper right-hand corner of your eclipse where perspectives are you have some kind of search box with the google logo next to it. What plugin is that?

    Thanks,

    DKD

  4. margelatu says:

    @DKD
    That search box is put there by Tasktop, which is a commercial plug-in built on top of Mylyn. It has a couple of nice features such as automated time tracking, e-mail integration, OS integration.

  5. Jim Flynn says:

    Thanks for pointing me at this plugin, it fills a gap in my Eclipse toolset that I had been trying to fill for a while without success with lame attempts at this area like coverlipse and codecover, EclEmma is the business.

  6. Stef says:

    Are there any new updates on Eclipse? I find Adobe Illustrator very helpful too but I still think Eclipse is a classic and I always go back to it.

  7. Stef says:

    Are there any new updates on Eclipse? I find Adobe Illustrator very helpful too but I still think Eclipse is a classic and I always go back to it.

  8. Indiebike says:

    Great article! You wrote it so well and the steps are just easy to follow. I have just shared this link to my friend who also works as a graphic artist.

  9. Bigshoes says:

    Thank you so much for sharing this. I shall try working on this. But I see this is way back year 2009. You don’t have any updates regarding this one, do you?

  10. Bhavin Shah says:

    Hi All,

    I am doing some RnD on Code coverage. I have used corbetura in my last project.

    My New project is extension of my last project and its requirement is I have to check only the code which is coded for this release in java file.

    In my java class i have place holder like
    /* Start Code Change */
    My New Code for project.
    /* End Code Change */

    Is there any way where i can just check coverage of the this many lines by doing some configuration or using some tool?

    Thanks,
    Bhavin

  11. eoloe says:

    Hi, I try using eclEmma with maven project to see coverage of JUnit tests. the problem is that it starts testing and shows 2 tests failed(although they don’t fail with normal tests start) and I can not export coverage report to any file format exept .es Tried installing emma-maven-plugin via maven plugin search in Eclipse but it didn’t help.
    Any ideas what might be the problem?

  12. I know this if off topic but I’m looking into starting my own weblog and was wondering what all is needed to get setup? I’m assuming having a blog like yours http://margelatu.org/2009/06/25/java-code-coverage-reports-in-eclipse would cost a pretty penny? I’m not very web smart so I’m not 100% positive. Any suggestions or advice would be greatly appreciated. Cheers

  13. Gretta Leon says:

    I think the coverage report is great feature of EclEmma.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">