Installing and using Code Coverage plugin in NetBeans 6.5

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:

code-coverage-1

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.

Activating Code Coverage plugin for a project

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.

codecoverage-test

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
Coverage results for one class

Coverage results for one class

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:

codecoverage-showreport

Project report looks like this:

Project Code Coverage Report

Project Code Coverage Report

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.

21 responses to “Installing and using Code Coverage plugin in NetBeans 6.5

  1. I din’t know about this… awesome…! thank you so much… I’ll use it in netbeans

  2. 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

    • 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

  3. 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.

  4. Code Coverage menu is deactivated in case of Netbeans Modules Suite?? How do I test for the complete Netbeans Java GUI application??

  5. 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)

    😀

    • I’ve had good luck testing Swing apps with UISpec4J and FEST-Swing (both open-source). FEST seems to support just about all Swing features, including drag and drop, but the tests run slowly. UISpec4J doesn’t support drag and drop entirely but the tests run much faster. I use UISpec4J whenever possible and FEST when necessary.

  6. 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.

  7. Hi there,

    I’d really like to use this in Netbeans 6.7.1.

    However, after installing the plugin and following your instructions, the ‘Coverage’ context menu for my project does not show. My project is a Maven based project, I guess this is why.

    Any chance this will work in that combination?

    regards,

    Tobias W.

  8. Great plugin! Although it seems that the plugin can only be used for java class libraries and not for ejb and web projects. For those kind of projects, the code coverage item on the contex menu is always disabled.

    • Yes, it seems so. Code Coverage plugin is based on Emma (emma.sourceforge.net) and it should be possible to configure Emma to analyze any Java project, including web and ejb. However, I didn’t do that yet.

  9. Very nice tool. Do you have any idea how to specify EMMA filters with the plugin? The EMMA documentation shows how to specify filters with command line options or Ant task attributes, but it doesn’t show a way to set filters with a system property. And I can’t find any way to configure the plugin itself.

    Thanks,
    Mike

  10. Too bad this plugin does not work with Maven projects. This makes NetBeans despite the excellent support for Maven projects less attractive for Maven projects than Eclipse.

  11. I’m trying to use this with some code that I’ve generated via javacc. I’ve verified that I see code coverage when I use the plugin on non-javacc projects. So I’ve got a good installation! When I try to run thru the Main in javacc, the coverage file never gets created. Have you heard of anybody else who had problems using EMMA with javacc?
    Thanks –
    Rick

  12. Hi

    I too faced the same problem what Tobias W. faced here. Pls let me know what to do.

    Ganesh

    Hi there,

    I’d really like to use this in Netbeans 6.7.1.

    However, after installing the plugin and following your instructions, the ‘Coverage’ context menu for my project does not show. My project is a Maven based project, I guess this is why.

    Any chance this will work in that combination?

    regards,

    Tobias W.

  13. good article. Thanks. This is exactly what i need to start working with test coverage plugin.

    Greetings from Poland😉

  14. Hi,

    I’ve tried this in NB 6.8, and it seems to work well with native Netbeans java projects, but the coverage menu item is disabled for Java Free-Form Projects

  15. Trying to use it with NetBeans 6.9.1. I see “EMMA” collecting data during the testing phase, but when I open classes or look at the code coverage summary, it says that nothing is covered. What am I missing?

    • Try to use different JDK version, for example. At my work we had to go back to JDK 6 update 21 as newer versions didn’t work. Let me know if it worked.

  16. Hi
    I have installed the ‘Code Coverage’ plugging in my netbeans IDE. The project that I am working on is Maven project. I tried to follow the steps you have given in this site, but I Could not activate the Code Coverage as you mentioned in my project. The Menu item Coverage is coming but it is disabled. What changes are required to enable to Coverage option for my project ( Maven project).

    Br
    Sajo John

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s