Coverage Testing

Adam Myatt, Sang Shin, sang.shin@sun.com, www.javapassion.com/javaprogrammingadv

 

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.


Change Log



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)


In this exercise, you are going to perform a simple code-coverage testing on a simple class.  This exercise is based on the Reviewing the NetBeans Unit Tests Code Coverage Plugin article by Adam Myatt.

(1.1) Create a new project


1. Create a new project.

Figure-1.11: Create a new project

Figure-1.12: Ceate Java Class Library project

Figure-1.13: Give a project a name

2. Add a new Java class.

Figure-1.14: Create a new Java class

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.

Figure-1.21: Create JUnit test

Figure-1.22: Choose JUnit 4.x

Figure-1.23: Create Tests

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.

Figure-1.26: Do the test

Figure-1.27: See the testing result

                                                                                                                                    return to top of the exercise


(1.3) Perform coverage testing


1. Activate the coverage testing.

Figure-1.31: Activate Coverage Collection

2. Run the JUnit test.
3. Observe the coverage testing in the StringUtils.java.

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. Perform the JUnit test.
2.  Observe the coverage testing result.

Figure-1.61: Coverage result

                                                                                                                                    return to top of the exercise


(1.7) Display coverage testing statistics


1. Right click project node and select Coverage->Show Project Coverage Statistics.


Figure-1.71: Show coverage statistics

2. Observe the Coverage statistics.


Figure-1.72: Coverage statistics

                                                                                                              return to top of the exercise

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.

          
           Figure-2.11: Create Anagram project


                                                                                                                       return to top of the exercise

(2.2) Activate the Coverage testing


1. Activate the coverage testing.


Figure-2.21: Activate Coverage Collection

                                                                                                                       return to top of the exercise

(2.3) Create JUnit test


1. Create JUnit test.

Figure-2.31: Create JUnit tests


Figure-2.32: Create Tests


Figure-2.33: Comment out fail()

                                                                                                                       return to top of the exercise

(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.

Figure-2.43: Display coverage statistics

Figure-2.44: Coverage statistics

                                                                                                                        return to top of the exercise


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>