Test Coverage with djUnit


Laurie Williams, Dright Ho, Ben Smith and Sarah Heckman. [Contact Authors]
CSC 326 - Software Engineering
Department of Computer Science
North Carolina State University

Back to Software Engineering Tutorials


0.0 Contents
1.0 Introduction to djUnit
2.0 Running djUnit
3.0 djUnit Coverage Report
4.0 Exercise
5.0 Resources

1.0 Introduction to djUnit

djUnit is an Eclipse plug-in that generates test coverage reports, tests with mock objects, and provide simple trace information about test cases. We will be using djUnit primarily as a coverage reporting tool. djUnit can be downloaded here. For more installation instructions, see the Installing Eclipse Plugins tutorial.

Coverage (from the Yellow Book) is defined as "a measure of completeness of the set of test cases" meaning that the more source code that is executed by your test cases the better. There are four types of coverage:

  • Method coverage: how many of your methods have been called by your test cases?
  • Statement coverage: how many of your statements have been run by your test cases?
  • Decision/Branch coverage: how many decision points (if statements) have evaluated both true and false?
  • Condition coverage: how many Boolean sub-expressions have been evaluated both true and false at a decision point?

For small programs, coverage is relatively easy to calculate by hand; however, for larger programs, the task is much more difficult. Coverage tools, like djUnit, help by automating the coverage process and providing readable reports.

djUnit may be used instead of JUnit. djUnit runs the JUnit test cases and generates the coverage report from the execution. djUnit provides overall, package, and file level information on statement and branch coverage.

Top | Contents

2.0 Running djUnit

2.1 There are two ways to run JUnit Test Cases or Test Suites.

2.1.1 You can right click on the test case class or test suite class and select Run As > djUnit Test.

2.1.2 You can select a test case or suite and click the arrow on the icon or select Run from the toolbar, and select Run As > djUnit Test.

2.1.3 You can select a test case or suite and click the arrow on the icon or select Run from the toolbar, and select Run... From here you will create a new djUnit test configuration, and name it. You can choose to run a single test case, or run all test cases in a project or folder.

Note: You should be careful here if you have FIT tests in the project. Running all of the test cases in the project will run those FIT tests and give you errors. The best idea is to just run the tests on the unittest/ source folder or whichever source folder contains your tests.

Top | Contents

3.0 djUnit Coverage Report

The djUnit coverage report is broken into three areas and only reports coverage statistics for statement (line) and branch coverage. The first provides the overall coverage figures for the entire project. The second area provide coverage information about each package in the project. The last area provides coverage information about each file in the project. If you select the file name in the coverage report, the source code shown in the editor with indicators by the lines that are not covered by test cases.

The source file contains green and red icons on the left, that specify the lines that test cases do not hit. If you hover your mouse over the icon, the line number will be provided to you. Each line that was not hit by a test case is also highlighted with a yellow line under the code. The right part of the screen shows all the lines that were not covered by your test cases in the whole source file.

Top | Contents

4.0 Exercise

For this exercise we will be using the CoffeeMaker project. Download the CoffeeMaker project from here or import the CoffeeMaker project that you used in the JUnit tutorial. Unzip the CoffeeMaker project to your home directory and import the project into Eclipse. Please see the Rational XDE and Eclipse Import/Export Guide for instructions on how to import a project into Eclipse.

We all know that most computer scientists love caffeine, so the Computer Science department is looking to put a coffee kiosk in the new building. The coffee kiosk must be able to make coffee for students to purchase. CoffeeMaker User Stories and Black Box Test Cases.

The CoffeeMaker code is complete; however, we need you to create and run unit tests until you have 90% coverage and a green bar on the CoffeeMaker, Inventory, and Recipe classes. Run your JUnit tests using djUnit and generate a coverage report. (Hint: Run djUnit on the initial project to see what coverage you already have, and what lines you need to test.)

Deliverables to the TA

  • Run all of your JUnit unit tests, which give a green bar.
  • A coverage report will 90% statement coverage or higher for CoffeeMaker, Inventory, and Recipe classes.
Top | Contents

5.0 Resources
Top | Contents

Back to Software Engineering Tutorials
Unit Testing in Eclipse Using JUnit Tutorial ©2003-2009North Carolina State University, Laurie Williams, Dright Ho, Sarah Heckman, and Ben Smith.
Email the authors with any questions or comments about this tutorial.
Last Updated: Monday, March 30, 2009 3:03 PM