Code Coverage in Netbeans 6.1

hundredhonkΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 5 χρόνια και 2 μέρες)

319 εμφανίσεις

Code Coverage in Netbeans 6.1
Holger Paffrath – August 2009
Code coverage is a simple method used to determine if your unit tests have covered all relevant

lines of code in your program.
Netbeans has a plugin that uses EMMA, an open source toolkit for measuring and reporting Java

code coverage.
This tutorial will go through how to install the Netbeans Code Coverage plugin and how to use it to

test a simple Java class.
Note : That the code coverage tool is not available for Java CAPS Repository based projects.
Installation
Select “Tools/Plugins” from the Netbeans Menu.
Select the “Settings Tab” then select the “Netbeans Beta” checkbox.
Select the “Avialable Plugins” tab and press the “Reload” button to reload all the plugins.
Select the “Code Coverage” plugin from the list and click the “Install” button.
Accept the license agreement and install.
Click “Close”, the Code Coverage plugin is now installed.
Using the Code Coverage Plugin
Lets create a simple project to highlight the code coverage tests.
Create a new Java Application Project. We are going to create one called “CodeCoverageTest”.
Replace the main.java with a new file called “CodeCoverateTest.java” and copy and paste the

following code
package codecoveragetest;
public class CodeCoverageTest {
/**
* @param args the command line arguments
*/
public static void CodeCoverageTest(String arg) {
if (arg.equals("True")){
System.out.println("True");
}else{
System.out.println("False");
}
}
}
As you can see, the code is pretty simple, we are just going to test the code.
Save your source. Now to create the test.
Right click on the “CodeCoverageTest.java” file and select “Tools/Create Junit Tests”
Select “Junit 4.x” and select “OK” on the “Create Tests” window. We are going to accept the

defaults.
You will now see your Junit test in the project.
Open the file “CodeCoverageTestTest.java”
Change the CodeCoverageTest test to the following
/**
* Test of CodeCoverageTest method, of class CodeCoverageTest.
*/
@Test
public void testCodeCoverageTestTrue() {
System.out.println("CodeCoverageTest");
String arg = "True";
CodeCoverageTest.CodeCoverageTest(arg);
}
Note that we are just passing the value “True” which according to our code, should display “True”.

Remember, we are not trying to do Java programming here, we are just testing the code coverage

tool.
Run the test to make sure it works by Right clicking “CodeCoverageTestTest.java” and select “Run

File”. Your test should run with a pass.
Now to activate the Code Coverage Plugin.
Right Click on the “CodeCoverageTest” project and select “Coverage” then “Activate Coverage

Collection”.
Now everytime we run a unit test, our code coverage details will be shown.
Run your test again to see the details.
Click on your source code “CodeCoverageTest.java” and you should see Green lines through the

executed code.
Note that the “else” statement isn't highlighted – that's because it wasn't executed.
If you right click again on your project and select “Coverage” then select “Show Project Coverage

Statistics”
The following view showing how much of the source code was tested is shown.
As you can see, we have covered all classes, but only 60% of the code in the class. Lets fix that

now.
Add a new test to your Junit Test...
/**
* Test of CodeCoverageTest method, of class CodeCoverageTest.
*/
@Test
public void testCodeCoverageTestTrue() {
System.out.println("CodeCoverageTest");
String arg = "True";
CodeCoverageTest.CodeCoverageTest(arg);
}
@Test
public void testCodeCoverageTestFalse() {
System.out.println("CodeCoverageTestFalse");
String arg = "False";
CodeCoverageTest.CodeCoverageTest(arg);
}
Re-run your test.
If you look at the “CodeCoverageTest.java” file, you will now see that the “else” statement is now

covered as we have covered it in the second test.
If you check the statistics you will find that we are still not 100% line coverage, but the remaining

lines as seen above are not important. Its the closing bracket and the else statement.