Coverage Testing

In this lab, you are going to exercise basic features of NetBeans code
coverage plug-in. The code
coverage must be enabled for each project by invoking the Coverage |
Activate Coverage Collection action from the project node's context
menu. Then, as unit tests are run, the plug-in will update the code
coverage data and markup the editor's lines accordingly. The lines
which are fully covered by unit tests are annotated with green color in
java editor and the line covered partially are annotated in yellow. The
colors can be modified in Tools | Options | Fonts & Colors |
Annotations. Reports are also supported. You can view coverage
reports for
your project by clicking YOUR_PROJECT_NODE->Coverage->Show
Project Coverage Statistics menu item.
Expected duration: 60 minutes (not including time
for homework)
Software
Needed
Before you begin, you need to install the following software on your
computer.
- Java Standard Development Kit (JDK™) version 5.0 (download) or
6.0 (download)
- Select JDK 6 Update x
from the download page
- NetBeans IDE 6.0 (download)
- Download either "Web &
Java EE" or "All"
bundles.
- NetBeans code coverage plug-in.
- For your convenience, it is included in the hands-on lab zip
file under <LAB_UNZIPPED_DIRECTORY>/javatestcodecoverage/netbeansplugin directory.
- 1078_javatestcodecoverage.zip (download)
- The zip file contains this document and the lab contents.
- Download it and unzip in a directory of your choice.
Change Log
- March 14th, 2008: Created
Lab Exercises
Exercise 0: Install NetBeans code coverage
plug-in
1. Select Tools from top-level menu and select Plugins.

Figure-0.11: Select Plugins
2. Select Downloaded tab and click Add Plugins button.

Figure-0.12: Add plugins
3. Browse down to
<LAB_UNZIPPED_DIRECTORY>/javatestcodecoverage/netbeansplugin directory and select
both nbm files and click Open.

Figure-0.13: Select plugins
4. Check both EMMA and Code Coverage Plugin and click Install.

Figure-0.14: Install the plugins
5. Click Next.

Figure-0.15: Install plugins
6. Check "I accept the terms .." and click Install.

Figure-0.16: Accept licensing terms
7. Click Continue.

Figure-0.17: Validation warning
8. Follow the remaining instructions as mentioned.
Exercise 1: Do coverage testing (using
standalone codecoverage application)
(1.1)
Create a new project
1. Create a new project.
- Select File from top level menu items and select New Project.

Figure-1.11: Create a new project
- Select Java under Categories and Java Class Library under
Projects.
- Click Next.

Figure-1.12: Ceate Java Class Library project

Figure-1.13: Give a project a name
2. Add a new Java class.
- Right click project node and select New->Java Class.

Figure-1.14: Create a new Java class
- Observe that the New Java Class dialog box appears.
- For the Class Name field, enter StringUtils.
- For the Package field, enter mypackage (or whatever package name
you want to give).
- Click Finish.

Figure-1.15: Give a name to the new Java class
3. Modify the IDE generated code with the code below.
package mypackage;
/**
*
* @author sang
*/
public class StringUtils {
public static String tryIntToString(int numLoops) {
StringBuffer sb = new
StringBuffer();
for (int i = 0; i <
numLoops; i++) {
sb.append(String.valueOf(i));
}
return sb.toString();
}
}
|
Code-1.16: Modified StringUtils.java

Figure-1.17: Modified StringUtils.java
return to top
of
the exercise
(1.2)
Create JUnit test
1. Create JUnit test.
- Right click StringUtils.java and select Tools->Create JUnit
Tests.

Figure-1.21: Create JUnit test
- Select JUnit 4.x (if it has not been selected already).
- Click Select button.

Figure-1.22: Choose JUnit 4.x
- Observe that Create Tests dialog appears.
- Click OK accepting defaults values.

Figure-1.23: Create Tests
- Modify the IDE generated testing code as shown below.
package mypackage;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author sang
*/
public class StringUtilsTest {
public StringUtilsTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test
public void
testTryIntToString() {
System.out.println("tryIntToString03");
int numLoops = 10000;
String result = StringUtils.tryIntToString(numLoops);
int expectedResult = 38890;
assertEquals(expectedResult, result.length());
}
}
|
Code-1.24: Modified StringUtilsTest.java

Figure-1.25: Modified StringUtilsTest.java
2. Perform the JUnit test.
- Right click the project node and select Test.

Figure-1.26: Do the test
- Observe that the test passed.

Figure-1.27: See the testing result
return to top
of
the exercise
(1.3)
Perform coverage testing
1. Activate the coverage testing.
- Right click project node and select Coverage->Activate Coverage Collection.

Figure-1.31: Activate Coverage Collection
2. Run the JUnit test.
3. Observe the coverage testing in the StringUtils.java.
- Open StringUtils.java.
- Observe the lines of code that were executed are highlighted with
light green colored shade.

Figure-1.32: Observe the coverage
return to top
of
the exercise
(1.4)
Add another target class
1. Create another Java class called StringUtils1.java.
2. Modify the IDE generated StringUtils1.java as shown below.
package mypackage;
/**
*
* @author sang
*/
public class StringUtils1 {
public static String tryIntToString(int numLoops) {
StringBuffer sb = new StringBuffer(numLoops);
if (numLoops > 10000) {
sb.append("greater than 10000");
} else {
for (int i = 0; i < numLoops; i++) {
sb.append(String.valueOf(i));
}
}
return sb.toString();
}
}
|
Code-1.41: Modified StringUtils1.java

Figure-1.42: Modified StringUtils1.java
return to top
of
the exercise
(1.5)
Create JUnit test for the newly created class
1. Create JUnit test for the StringUtils1.java.

Figure-1.51: Create JUnit test
2. Modify the IDE generated Junit testing code as shown below.
package mypackage;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author sang
*/
public class StringUtils1Test {
public StringUtils1Test() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of tryIntToString method, of class
StringUtils1.
*/
@Test
public void tryIntToString() {
System.out.println("tryIntToString");
int numLoops = 10000;
String result =
StringUtils1.tryIntToString(numLoops);
int expectedResult = 38890;
assertEquals(expectedResult,
result.length());
}
}
|
Code-1.52: Modified StringUtils1Test.java

Code-1.53: Modified StringUtils1Test
return to top
of
the exercise
(1.6)
Perform coverage testing
(1.7)
Display coverage testing statistics
1. Right click project node and select Coverage->Show Project
Coverage Statistics.
Summary
In this exercise, you learned how to
perform a simple coverage testing.
return to the
top
Exercise 2: Do the code coverage testing
with Anagram application
In this exercise, you are going to
perform coverage testing Anagram sample application.
(2.1)
Create a new project
1.Create a new project.
- Select File from top-level menu and select New Project,
- Select Java under Samples and select Anagram Game under Projects.
- Click Next.

Figure-2.11:
Create Anagram project
return to top
of
the exercise
(2.2)
Activate the Coverage testing
1. Activate the coverage testing.
- Right click project node and select Coverage->Activate
Coverage Collection.
(2.3)
Create JUnit test
1. Create JUnit test.
- Right click Anagrams.java and select Tools->Create JUnit Tests.
(2.4)
Perform coverage testing
1. Right click project node and select Test.

Figure-2.41: Perform test
2. Open Anagrams.java and observe the colored lines.

Figure-2.42: Observe the coverage testing result
3. Display the coverage statistics.
- Right click project node and select Coverage->Show Project Coverage
Statistics.

Figure-2.43: Display coverage statistics
- Observe the coverage statistics is displayed on the right side of
the IDE.

Figure-2.44: Coverage statistics
Summary
In this exercise, you did perform
coverage testing on Anagram sample application.
return to the top
Homework
exercise (for people who
are taking Sang Shin's "Advanced Java Programming online course")
<tbd>