Unit Testing in Jazz Using JUnit
Laurie Williams, Dright Ho, Ben Smith and Sarah Heckman [Contact Authors] |
| 1.0 | Introduction to JUnit |
| 2.0 | Creating a Test Class |
| 3.0 | Creating a Test Suite |
| 4.0 | Running JUnit Test Cases |
| 5.0 | Assertion Statement Reference |
| 6.0 | Exercise |
| 7.0 | Resources |
JUnit is an open source unit testing framework for automatically unit testing Java programs. JUnit provides functionality for writing and running unit test cases for a project under development, and is helpful when doing Test-Driven Development (TDD). This tutorial describes how to use JUnit within the Jazz environment, but most of the information except for running the JUnit test cases is generic to any JUnit set up. See http://www.junit.org/ for directions on how to run JUnit test cases outside of the Jazz environment. Jazz is built on the Eclipse platform and comes with JUnit built into the Workbench. You can quickly create test case and test suite classes to write your test code in. With Jazz, Test-Driven Development (TDD), becomes very easy to organize and implement. Typically, you create the application class that you want to test first. Then a tester can create the associated test case which provides a one-to-one correspondence between application classes and test classes. However, a tester can create test classes individually and later tie them to application classes. By using the built in functionality to create JUnit classes, the test cases are created with the needed imports and extensions for JUnit to run. Once the test case class is built the actual test cases are then coded in by the tester. JUnit test classes can be rolled up to run in a specific order by creating a Test Suite. When a tester creates a test suite, Jazz will name it for you and will specify all of the test cases in the scope of the project that it can find. The code to run the test suite and to add all of the specified test cases you've created, is added to the test suite for you. There are two versions of JUnit currently available. JUnit 3.8 uses specific naming conventions for identifying test classes, methods, and suites. These naming conventions are described below:
JUnit 4.0 uses annotations (which are keywords that start with @) to identify test classes, methods, and suites. The key annotations are described below.
1.1 Import CoffeeMaker into Jazz We will be using the CoffeeMaker example through out this tutorial to highlight the main features of JUnit. CoffeeMaker is a small application that simulates the functionality of a coffee maker. The requirements for CoffeeMaker may be found here. Download the CoffeeMaker example from here. See Eclipse Import/Export for instructions on how to import CoffeeMaker into your Jazz workspace. 1.2 CoffeeMaker File Structure It is considered a best practice in testing, to separate the test case code from the application code. Typically the application code is in a source folder called src/ while the test code is in a source folder called unittests/ or tests/. Below is the file structure for the CoffeeMaker example that is used for as the exercise in this tutorial. You can emulate this file structure in your other Eclipse projects.
Most projects that you create will have a src/ folder that contain the main application code, a bin/ (which is not shown in the Package Explorer view) folder that contains the compiled .class files, and a lib/ folder that contains jar files that need to be on the project's build path. To run JUnit test cases the JUnit libraries must be on the classpath. JUnit 3.8 and JUnit 4 are integrated with Jazz; therefore, we can add the Jazz provided libraries to the project. 1.3 Putting JUnit libraries on the Project Classpath The JUnit library is required to be on the project classpath to compile and run the JUnit test cases. The classpath may be specific to the computer that you are on, so check to make sure that the JUnit library is set up correctly. If your JUnit library isn't set up correctly, there will be an warning message at the top of the Java Build Path window.
|
JUnit convention is that there is a test class created for every application class that does not contain GUI code. Usually all paths through each method in a class are tested in a unit test; however, you do not need to test trivial getter and setter methods. 2.1 There are many ways to create a JUnit Test Case Class.
2.2 The wizard for creating a new JUnit test case classes is displayed in Figure 5. If you use the method of creating a JUnit test case presented in step 2.1.6, most of the information below will be filled in for you, but may require minor modifications.
2.3 If you selected a Class under test you can click the Next button to select which methods you want to write test cases for. The method signatures will be created for you. Click Finish and the new test case class will open in the editor. 2.4 Below is a test case template from the JUnit 3 FAQ. This test class demonstrates the basic functionality of the setUp() and tearDown() methods, and gives example test cases. The testForException() method demonstrates how to test that an exception is properly thrown. Note: All source methods in the class under test must be public or protected, not private, in order to be tested by JUnit. If the method in the class under test is protected, the test class must be in the same package.
2.5 Below is a test case template based on the examples presented in the JUnit 4 FAQ. This test class demonstrates the basic functionality of the setUp() and tearDown() methods, and gives example test cases. The testForException() method demonstrates how to test that an exception is properly thrown. Note: All source methods in the class under test must be public or protected, not private, in order to be tested by JUnit. If the method in the class under test is protected, the test class must be in the same package.
|
A TestSuite is a simple way of running one class that, in turn, runs all test cases at one time. 3.1 There are four ways to create a JUnit Test Suite Class. First, select the directory (usually unittests/) that you wish to create the test suite class in.
3.2 Check to make sure that you are creating the TestSuite in the proper source folder and the proper package. Give the test suite a name. The default name is AllTests.java
3.3 Click Finish. The new test suite class will be open in the editor. 3.4 Below is a test suite template from the JUnit 3 FAQ. This test suite demonstrates the basic functionality of the suite() method, which is what you add each of the test cases to the suite in. This should all be generated for you by Eclipse if you use the first 3 methods in step 3.1 to create the Test Suite.
|
||||
The goal when running JUnit test cases is a green bar in the JUnit view. The green bar shows that all of the test cases passed. A red bar shows that one or more of the test cases failed. The stack trace of a test failure or error are displayed below the listing of the test cases run. Figures 7 and 8 show the green and red bars in the JUnit view.
4.1 There are three ways to run JUnit Test Cases or Test Suites.
|
In JUnit, a test occurs when ever you assert that something should be a specific value. Each JUnit test case (method) should have at least one of the assert statements listed below, otherwise the test passes, which can be misleading if you never actually check the value of any data. The most common assert statement is assertEquals() which takes at least two arguments. The first argument is the expected value of some piece of data at that point in the test case. The second argument is the actual value of that data, usually obtained by a method call, at that point in the test case. The method will check for equality between the two pieces of data. For a custom object this means that the equals() and hashCode() methods should be overridden. Otherwise, the assertEquals() method will check for equality of the objects location in memory. These will be different because the expected object is created within the test case while the actual object is created within the application. If any of your assert statement fail, the test progress bar in the JUnit view will be red. Additionally, a failing assert statement will cease execution for that test case. Therefore, assert statements after that point will not be executed. This is a list of the different types of assertion statements that are used to test your code. Any Java data type or object can be used in the statement. These assertions are taken from the JUnit API.
|
For this exercise we will be using the CoffeeMaker project. Unzip the CoffeeMaker project to your home directory and import the project into Jazz. Please see the Eclipse Import/Export Guide for instructions on how to import a project into Jazz (which is built on Eclipse). We all know that most computer scientists love caffeine, so the Computer Science department is looking to put a coffee kiosk in EBII building. The coffee kiosk must be able to make coffee for students to purchase. Take a look at the CoffeeMaker User Stories/Requirements to look for boundaries and other things to test. The CoffeeMaker code is complete; however, we need you to create and run unit tests on the following user stories: 1) Add a Recipe, 2) Delete a Recipe, 3) Edit a Recipe, 4) Add Inventory, 5) Check Inventory, and 6) Purchase Coffee. One test class has been created for you: CoffeeMakerTest under the unittests/ directory. You can create RecipeTest and InventoryTest classes as well. There are currently 5 (very obvious) bugs in the system. We need you to generate enough unit tests to find 2 of the 5 bugs. Once you find the bugs, create a fix (These should be very simple fixes. If the fix takes longer than 5 minutes, you found a bigger bug than the one we wanted you to find! - and you should let your TA know). Create a list of the bugs that you find, and submit the CoffeeMaker project to your TA with a green bar! Deliverables to the TA
|