(Source Code) How to save the data in a url location to a file using java
Source Code : 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 result 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
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.
- guru's blog
- Login to post comments
