Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

EECS3311

LAB 4: Code Coverage Exercise

Exercise 1: API Unit Testing using JUnit

The system we will be using is JFreeChart. JFreeChart is an open-source Java framework for chart calculation, creation, and display. This framework supports many different (graphical)    chart types, including pie charts, bar charts, line charts, histograms, and several other chart   types.

Project prep

1.   Import a new project from git

a.   git clonehttps://github.com/jfree/jfreechart.giton your directory.

b.   Go to File -> Import …

c.   Select Maven -> Existing Maven Projects -> Next

d.   Browse to the location you cloned -> Check the pom.xml -> Finish.

2.   Refresh so all the dependencies are downloaded.

Create a Simple JUnit Test

To create a test suite containing a single unit test in JUnit, follow these steps:

1.   In the package explorer, expand the src/main/java to all the packages of the project.

2.   Expand the org.jfree.data package within the archive to show all the files contained in that package.

3.   Finally, expand the Range.java item to expose the class contained in that file, along with all the class methods and fields contained in that class.

4.   Right-click on the Range class (which has a green ‘C’ icon, denoting it is a class), and select  New -> JUnit Test Case. Type RangeTes2t in the Name field. Make it a JUnit 4 test

5.   Click Finish.

6.  As a practice, write a simple test case for the getCentralValue() method:

7.   Now that you have a completed test case, run the test class. To do this, right-click the RangeTest class in the Package Explorer and select Run As -> JUnit Test.

8.  This will change the perspective to the JUnit perspective, and run all the tests within the RangeTest class. The test just written should pass, as indicated by the JUnit view, similar to the figure below.

 

Exercise 2: Code Coverage Measurement

Objectives

The objective of this exercise is to introduce students to the concepts of determining the adequacy of a test suite based on its coverage of the code. In white-box testing, it is important to measure the adequacy of a test suite based on completeness defined by the portion of the   code which is exercised. This definition can take several forms, including control-flow coverage criteria: e.g., statement (or node) coverage, decision (or edge) coverage, condition coverage,   path coverage, or data-flow coverage criteria: e.g., DU pairs coverage.

After completing the exercise, students will be able:

●   To use code coverage tools to measure test adequacy and become aware of similar tools for other programming environments

●   To design test cases to improve code coverage

●   To understand some of the benefits and drawbacks of measuring test adequacy with code coverage tools

In this exercise, we will be using EclEmma Java Code Coverage, which is the default testing tool provided within the Eclipse IDE.

Instructions

1.   On the same project and same test class (RangeTest2.java)

a.   right-click -> Coverage as -> click JUnit Test

 

2.  This is an example figure of Range (middle left) and RangeTest2 (middle right) after running RangeTest2.

a.   Left col: test methods or class that is executed.

b.   Middle col: open any file to check the covered (green) and uncovered (red) lines by the test.

c.   Right col: general outline of the opened file.

d.   Bottom row: shows the coverage rate of the test w.r.t all the components in the package.

3.  As we can see, all the lines of the unit test case are covered for RangeTest2, since that is what we are running. The part that we are interested in is the focal method (target method under test), which is in Range.java. You can see that the constructor and the getCentralValue() method are covered by the current unit test case.

4.   Using this tool, you can get different coverage metrics by clicking the kebab menu (the  three dotted icon) on the right, as shown in the figure below. Navigate the element to org.jfree.data.Range under src/main/java to see the coverage metric you have selected.

 

5.   Click on the different counters to calculate different code coverages.

6.  Add more meaningful unit test cases to increase different code coverage (brach, line, method) for Range the class in RangeTest2.java.

7.   Compare your test cases with the developer-written test cases (org.jfree.data.

8.   RangeTest.java) w.r.t. different code coverage for Range Class that is in RangeTest.