NetBeans Code Coverage Plugin
Outline
In this tutorial you'll follow a short walkthrough for
using the NetBeans Code Coverage Plugin named "TikiOne
JaCoCoverage".
- Introduction
- Installing the plugin
- Running the Tests
- Viewing Coverage Results
- Improving Coverage
- Known Issues
Introduction
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. Of course you need to have tests in your
project for it to work.
NOTE: There is a separate page for code coverage of Maven
projects - MavenCodeCoverage
Demo Project
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 7.3 on Ubuntu.
Install the plugin
If you haven't already install the plugin follow these
simple steps.
From NetBeans menu select Tools > Plugins and wait for
the Plugins dialog to appear. Select the
"Available Plugins" tab an type "coverage" in the Search
box. The TikiOne plugin appears in the left hand
panel.
Click the checkbox next to the plugin name and click
install.
Running the Tests
After you have opened the demo project expand the project
node to see the files in the Source package. In the
default package, open PigLatin.java.
Once the plugin is installed you will see a new menu item
from the Project context menu "Test with
JaCoCoverage". Choosing this option will run the
project with all the unit tests and gather coverage
data. The tests should pass.
Viewing Coverage Results
Once the tests are completed a browser window will open
automatically to display the coverage results.
Notice coverage of the package is 63%. Click on the link
for the package "Default" to view its classes. Click
on the "PigLatin" class to view its methods. Click
on the "convert(String)" method to view the code.
Observe green highlighting in the results window. Notice
the true branch of the 'if' statement is green, but the
false branch is not. Specifically, lines 26 & 28 in
the source are red. That's because the test didn't cause
those lines to be executed.
Improving Coverage
Back in NetBeans, go to the Test package and 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.
Once again run "Test with JaCoCoverage".
In the browser results window notice coverage is now
100%.
Expand the results to view the PigLatin source and 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.)
That's all there is to viewing code coverage of your
tests!
Known Issues and Quirks
- The plugin doesn't work with Maven-based projects,
only Ant-based ones. (Bug 157946)
- You can't obtain coverage from a single unit test only
from the entire project tests.
- 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.
- More issues are discussed in the User Comments on this
page.
- On the plugin developer project
page there is a statement that the plugin is no
longer under active development.
|