In this tutorial you'll follow a short walkthrough for using the NetBeans Code Coverage Plugin.
- Activating Coverage
- Viewing Coverage Results
- Showing Coverage Statistics
- Known Issues
The NetBeans Code Coverage Plugin provides an interactive way to see testing coverage results within the NetBeans IDE. This enables developers to quickly identify the portions of Java code that aren't covered by their unit tests. The plugin is part of the standard distribution so there's no need to install anything to use the plugin. Of course you need to have tests in your project for it to work. If not just use the Demo project so you can have a preview.
NOTE: There is a separate page for code coverage of Maven projects - MavenCodeCoverage
This tutorial offers a walkthrough that uses a demonstration NetBeans project.
Download and unzip the CodeCoverageDemo project into a local folder.
The tutorial provides screenshots so you can still see how the main features operate even if you choose not to follow the step-by-step walkthrough.
This walkthrough was created with NetBeans 6.5 on Ubuntu.
You must "activate" the plugin for the project on which you intend to use it. There is no "global" activation; each project requires a separate activation step.
To activate the plugin, open the CodeCoverageDemo project and right-click on the project node in Projects View.
From the context menu, click on Coverage -> Activate Coverage Collection. (If no Coverage menu item appears, the plugin has not been installed properly).
Expand the project node to see the files in the Source package. In the default package, open PigLatin.java.
Viewing Coverage Results
From the Run menu, choose "Test". (Alt-F6)
The JUnit tests window should say "EMMA: collecting runtime coverage data ..." The tests should pass.
Observe green highlighting in the source code window. Notice the true branch of the 'if' statement is green, but the false branch is not. Specifically, lines 26 & 28 in the source aren't green. That's because the test didn't cause those lines to be executed.
Show Coverage Statistics
If you are using JDK 6, from the Coverage menu select "Show Project Statistics."
Notice coverage of PigLatin.java is 82%.
In the Test package, open PigLatinTest.java.
Notice lines 11 and 12 are commented out. Those two lines will cause the false branch of the 'if' statement to execute.
Remove the slashes to uncomment those two lines. Alt-F6 to Test Project.
View the PigLatin editor window and observe the green highlighting now includes line 26 & 28.. (The 'else' statement is not green, apparently because the plugin doesn't consider it an executable statement.)
If you Show Project Statistics again, it says 100%.
Known Issues and Quirks
- The plugin doesn't work with Maven-based projects, only Ant-based ones. (Bug 157946)
- Test-Single DOES NOT seem to work reliably. Use Test Project instead. (Bug 163402)
- Show Project Statistics is only available with JDK 6 or later.
- If you also have the Checkstyle plugin installed, checkstyle errors will hide the coverage highlighting. So fix the checkstyle errors and then you can see the coverage highlights.
- If you have a class with only static methods, you can't get 100% coverage unless you also instantiate the class.
- This plugin is based on the open source EMMA tool. For more info, visit the EMMA home page at http://emma.sourceforge.net/index.html