Code Coverage with EclEmma:
Code coverage is one of my personal favorites when it comes to tracking how good my JUNITS have fared. Anyways let me give you a bird’s eye view of what code coverage is all about. Let’s day I’ve got code that encapsulates my business logic & I need to check out if the Business logic I have embedded in it is correct what do I need ?? No prices for guessing I gotta wire up some JUNITS to test out the Business logic.
Alright so I’ve got Test Cases now. But how do I know that test cases check all the possible scenarios. Well that’s where the code coverage checks the line coverage i.e to check the lines of code that have been traversed in the code. Branch coverage checks the alternate flows like if ; else ; exceptions that kinda stuff. So I’m not going to get into that.
Some links that kinda helped my learning are like listed below:
Anways EMMA which is a project on the SourceForge.net site is a huge hit, an amazing set of ANT based tools that help developers check the code coverage. I’ve tried to use it in the past but hey it can get tricky.
Anyways all that is history with this amazing plugin for eclipse. I have tried the Emma plugin on IntelliJ and that was kinda neat so decided to see what eclipse has to offfer.
Anyways here goes,
The site
http://www.eclemma.orgThey’ve got an update site and makes the installation like a walk in a park.
So after successful installation there’s a neat coverage icon that lets you come up a Run configuration for the code coverage.
That gives you two options:
- Coverage as –> Java Application (A class with public static void main(String args[]))
- Coverage as –> JUnit Test

Anyways I’ve got simple project a Java Project with a couple of Simple Classes and some EJBs, logic like Hello World that kinda stuff.
Fig 1
roject Structure
P.S this a simple java project be sure to have a Dynamic Web Project to check out the EJB’s Test case and stuff like that.
Fig 2 : Coverage Icon
So after the installation’s successful there’s this icon that you get to see. Beware you just might miss it and wind searching for it. Anyways this guys turns up by default in the Java Perspective and so be sure to have that as the default.
After that to run coverage on the a given file/ module be sure to select the file open the Java editor and click on the Coverage Icon. The 2 default options are listed.
What happens next after the code is executed is a the class files are instrumented and a serializable file usually the .ec or .ser is generated but hey why would you after all you ain’t writing the build scripts on this one.
After the Junit are executed and the coverage is generated Eclipse opens up a Coverage View.

By default this view shows the list of classes, packages for the entire project.
You might want to use the filters available to view classes that are more specific for the testing purposes.
From the View the user gets to see the code coverage at both package & class levels. You can drill down to the lowest levels in the class ie Class –> Methods –> Lines in the code even.
Fig 3 : Coverage View

Figure 4 : Different Components of the View

So another neat feature is the class wise coverage.
Let’s say my code has been tested how do I check the levels of line coverage(excluding the JavaDocs)
Simple after the Coverage has been run on the project by default Eclipse has a Display renderer for this plugin which indicates on the Java Source code the lines that have been covered and the lines that have’nt been covered. Green means the code’s been test and red indicates otherwise.
This can alternatively be viewed as a HTML report which also offers the same kind of view.
Figure 5 – Line Coverage indicators
Now to view the report in standard format. Most of us are familiar with the HTML xsl report that EMMA uses. So how do I export my report from Eclipse to HTML.
That’s easy just Right click on the Coverage view and you get the options to Import / Export.
Just check out the illustrations on Figure 6, 7 and that’s about exporting the report.


Figure 6, 7 – Exporting the Report to HTML
This HTML report needs no introduction. This has been an amazing view to a great tool used across the globe by teams to check their code coverages effectively.
Figure 8 : Exported Emma Report HTML View From IE
So that sums up the amazing eclipse plugin that is EclEmma do be sure to check it out.
P.S I also like a couple of other coverage tools Cobertura, Clover, DJUNIT but that’s another article. Adios !!!