Lecture 18 Implicit Promotion Last time 1 Implicit type promotion 2 this Today 1 Unit testing and JUnit 2 Constructors revisited 3 equals 4 Mutable and immutable methods 10 11 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Project 4 Will Be Assigned This Afternoon It is due Friday 10 20 at 11 pm The project is closed You must complete the project by yourself Assistance can only be provided by teaching assistants TAs and instructors You must not look at other students code Start now Read entire assignment from beginning to end before starting to code Check out assignment now from CVS Follow the instructions exactly as much of grading is automated CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Unit Testing So far projects have consisted of a single class e g PicViewer and a driver Java programs typically contain many classes Locating errors can be tricky if multiple classes involved Unit testing helps overcome this problem Unit testing test each class unit individually Goal is to eliminate errors within classes CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 How To Do Unit Testing Needs for unit testing One possibility write a driver for each class Method for defining tests inputs expected outputs Method for running tests Method for reporting results Driver class contains main method main method creates objects in class to be tested calls methods prints outputs User checks outputs determines correctness Good easy no special tools needed Bad tedious relies on human inspection of outputs Another approach JUnit CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 JUnit A unit testing tool for Java Includes capabilities for Test definition including output checking Test running execution Result reporting Seamless integration with Eclipse Note In this class we will use JUnit 3 8 1 A newer version JUnit 4 0 has recently been released but not yet evaluated by the CS dept JUnit 4 0 includes more features than JUnit 3 8 1 but basic principles are the same CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 Structure of a JUnit 3 8 1 Test Case JUnit library import junit framework TestCase Test case name Needed will see why later in semester public class FunnyIntegerSetTest01 extends TestCase public void testInsert FunnyIntegerSet set new FunnyIntegerSet set insert 3 assertTrue set null Tests public void testFindClosest FunnyIntegerSet set new FunnyIntegerSet set insert 3 set insert 6 assertEquals 6 set findClosest 5 Assertions result checkers CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 5 A Test Case Is A Class Inherits from extends class TestCase which is in library junit framework TestCase we will learn about inheritance later Contains methods like any class Some method names begin with the word test Method bodies contain calls to assertion checkers E g assertTrue assertEquals Assertion checkers are also defined in the above library CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 6 A Test Case Is A Collection of Tests In example a class FunnyIntegerSet is being tested by the test case Terminology SUT software under test Test case methods beginning with test are viewed as individual tests that the SUT either passes or fails Passing failing determined by assert calls assertTrue b If b is true keep running test otherwise halt test report fail assertEquals expected actual If expected actual equal keep running test otherwise halt test report fail If test terminates without failing it passes Test may create objects call other methods etc just like any other method CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 7 Example Revisited FunnyIntegerSet is supposed to implement two operations on sets of integers public void insert int i add i into set public int findClosest int i return element of set closest to i What to test Does insert really work Does findClosest really work Does inserting yield a non empty set etc Does it return the closest value in a two member set Does it return the only member of the set in a one member set etc FunnyIntegerSetTest01 tests two of these properties CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 Running Tests in JUnit JUnit test cases are Java classes To run tests using JUnit a test runner must be specified They are stored in java files They may be manipulated like any other file Test runner executes each test in test case Results about passing failing reported Installation of JUnit typically identifies test runner to be used CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9 JUnit and Eclipse JUnit test cases may be created in Eclipse Select File New JUnit Test Case You may be prompted to install JUnit if it is not already installed Fill in Name Class Under Test Click Next and CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 JUnit and Eclipse cont select tests for which test method stubs should be created Click Finish Resulting java file is CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11 Java and Eclipse cont import junit framework TestCase public class FunnyIntegerSetTest02 extends TestCase public void testInsert fail Not yet implemented public void testFindClosest fail Not yet implemented fail is another assert method You can replace fail with whatever you like CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 12 Running Tests in Eclipse Right click in Package Explorer Select Run As JUnit Test inside project New panel appears showing results of running all tests in same project CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 13 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 14 Hints on Testing Give names to tests that relate to class being tested Develop some tests before you code Helps you to clarify what you are supposed to be doing Gives you some ready made tests to run while you code Use tests to debug How many tests Statement coverage develop tests to make sure each statement in class is executed at least once including constructors Decision coverage develop tests to make each condition if statement in program both true and false You should at least reach statement coverage in your own testing CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 15 Constructors Revisited Constructors are special object creation routines inside classes They are called using new Date d new Date Overloading may be
View Full Document