One of techniques to ensure that software is properly and thoroughly tested is to get code coverage information and find which blocks of code weren’t executed at all.
There is a number of code coverage tools for Java applications. One of them, free and mature is Emma ( http://emma.sourceforge.net/ ) which is one used by NetBeans code coverage plugin.
Installing plugin
To install plugin, select Tools -> Plugins and choose “Code Coverage” in the list of available plugins:
After accepting license terms, plugin is ready for use.
Activate Code Coverage plugin in a project
To activate plugin, right click on project name in Projects view and click on Coverage -> Activate Coverage Collection.
Using Code Coverage Plugin
I’ll try to demonstrate usage of Code Coverage plugin with one simple example. Create a new project and add following class to it:
package coverage; public class Main { public void testMethod() { verify(1); } private String check(String a) { return ""; } private String verify(int i) { StringBuffer retVal = new StringBuffer("verified."); if(i == 0) { retVal.append("i == 0"); } if(i == 1) { retVal.append("i == 1"); if(i == 2) { /* partial coverage */ retVal.append("i == 2");} } return retVal.toString(); } }
To test it, in test packages create following class, in the same package (coverage):
package coverage; import junit.framework.TestCase; public class MainTest extends TestCase { public MainTest(String testName) { super(testName); } public void testOne() { Main m = new Main(); m.testMethod(); } }
To see plugin in action, activate Code Coverage for a project as described above and run Test from context menu. When the execution finishes (it will be fast), we can check the results.
Seeing coverage results for a class
To see coverage results for one class, double click on it in Projects View. In the example below there are lines in two colors:
- green lines were covered completely
- yellow lines are executed partially – in example below, condition “if (i == 2)” was executed, but rest of the line was not
Project Coverage Report
Code Coverage plugin also provides report for entire project. To see it, right click on the project name and choose Coverage -> Show Project Coverage Statistics:
Project report looks like this:
In the report we can check package coverage for classes and lines of code and class coverage for individual classes.
Useful Links
Code Coverage [Wikipedia.org]
NetBeans Unit Tests Code Coverage plugin [NetBeans.org]
Getting Help
If you have problems with installing or running Code Coverage plugin, describe your problem in a comment and I’ll try to help. I can answer in English, French and Spanish.






7 responses so far ↓
lionh@rt // November 27, 2008 at 1:00 am |
I din’t know about this… awesome…! thank you so much… I’ll use it in netbeans
caesar // January 30, 2009 at 11:27 pm |
Hi!
Do you happen to know some way to use this plugin in functional testing a J2EE project with a client? I have a swing client which uses some webservices based on EJB3 classes on the server. I would very like to see what happens coverage-wise on the server side, if I execute something on the client side. The problem seems to be that this plugin only works with one project at a time. EMMA homepage suggests offline instrumentation in case of J2EE projects, but I’m not sure if this plugin can help in that or do I have to do everything manually?
Caesar
newsintegrator // February 2, 2009 at 3:25 pm |
Hi Caesar,
what is your app. server? I tried once to do something similar but it didn’t work for me. Now I am testing EJBs and below outside of application server. It is a good solution if there is not a lot of logic in EJB class.
boris
Roxana // April 28, 2009 at 9:59 am |
Hi,
I am trying the coverage on a few projects and I see different results:
1) On the JUnitSampleSol shows Project *is covered* green
2) On a couple of my projects shows Project *is _not_ covered* in red, although it shows the classes and lines covered and it colors the covered code.
3) On a RMI project shows no coverage at all although the project is covered; there is a line in the coverage output that says that the project contains classes without full debug information (Class_skel).
Any ideas about this behaviour?
Thanks,
Roxana.
Harini // June 15, 2009 at 5:46 am |
Code Coverage menu is deactivated in case of Netbeans Modules Suite?? How do I test for the complete Netbeans Java GUI application??
manuel // September 23, 2009 at 8:02 am |
Hi.
Is there any way to run an application in ‘code-coverage-mode’ as it can be done in Eclipse? i.e., without the need of jUnit tests.
My problem is that I have a Swing application, which is really really hard to test with jUnit. I’d like to turn on code coverage, exercise my application, turn off the code coverage and get the report.
(sorry if there are some errors in the post, I’m Spanish, but I prefer writing in English so others with the same problem can read it)
Collange Thomas // November 4, 2009 at 11:37 am |
Hello i have a problem to use this plugin with Netbeans 6.7.
When i rght click on a project the coverage choise is unusable .
Escuse me but i am french and i can’t speak in english very well.