Unit Testing in Eclipse Using JUnit
0.0 Contents1.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 |
Eclipse v. 3.1comes with JUnit built into the Workbench. Eclipse allows you to quickly create test case classes and test suite classes to write your test code in. With Eclipse, Test Driven Development (TDD), becomes very easy to organize and implement. The class that you will want to test is created first so that Eclipse will be able to find that class under test when you build the test case class. The test cases are built 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 programmer. The creation of test suites in Eclipse is even easier. When you create a test suite, Eclipse 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. Reminder of JUnit Naming Conventions:
It is considered a best practice in testing, to separate the test case code from the application code. It is also a good idea to separate your JUnit and FIT tests as well. Here is an example file structure that helps with this:
Most projects that you create will have a src/ folder that contain the main application code, a bin/ folder that contain the compiled .class files, and a lib/ folder that contains jar files that need to be on the project's class path. In order to use FitRunner, fit.jar needs to be on the class path. Since FIT requires the JUnit libraries, junit.jar must also be on the build path. The acctest/spec/ folder will contain the specification HTML tables of your FIT acceptance tests, while acctest/result/ will contain the HTML tables that show the results of your FIT acceptance tests. It is best to keep a clean file of your test specification so that you can rerun the tests on a clean file. The acctest/fixtures/ folder will contain the Java fixtures that run your FIT acceptance tests. The unittests/ folder will contain your JUnit test cases. The models/ folder may contain your models that are generated in Rational XDE. 1.1 Putting junit.jar on the Build Path
|
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 five ways to create a JUnit Test Case Class. First, select the directory (usually unittests/) that you wish to create the test case class in.
2.2 Check to make sure that you are creating the TestCase in the proper package. Give the test case a name.
2.3 If you selected a "Class Under Test" you can click the Next button, otherwise click Finish. You will be able to select which methods in the class under test that you want to write test cases for. The method signatures will be created for you. Click Finish. The new test case class will be open in the editor. 2.4 Below is a test case template from the JUnit 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 private, the test class must be in the same package.
|
A TestSuite is a simple way of running one program 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 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 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.
|
4.1 There are three ways to run JUnit Test Cases or Test Suites.
|
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. Download the CoffeeMaker project from here. 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. 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 acceptance tests on the following user storie requirements: 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 (some of them are based on requirements). We need you to generate enough unit tests to find these 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). Find and fix all 5 of the bugs. Create a list of the bugs that you find. Deliverables to the TA
|