1CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)Lecture 13:unit testingLast time:1.Method overloading2.thisToday:1.Unit testing and JUnitCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)1The problemProblems:need to be able to make sure all parts are testedneed to know in testing exactly which part was not as expectedneed to be able to keep the tests for modifications made laterUnit testing helps overcome this problems of making sure everything is tested Unit testing: test each class and each part of the class (unit) individually Goal is to eliminate inconsistencies between the API and the actual working of the codeCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)2Unit TestingUnit testing helps overcome this problems of making sure everything is tested in a structured wayUnit testing: test each unit individually (micro level – each method or specifically each interaction described in the API)Goal is to eliminate errors within classesNeeds for unit testingMethod for defining tests = inputs, expected outputsMethod for running testsMethod for reporting resultsOne possibility: write a driver for each classDriver class contains main methodmain method creates objects in class to be tested, calls methods, prints outputsUser checks outputs, determines correctnessGood: easy, no special tools neededBad: tedious, relies on human inspection of outputsAnother approach: JUnit2CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)3JUnitA unit-testing tool for JavaIncludes capabilities for:Test definition, including output checkingTest running (execution)Result reportingSeamless integration with EclipseNoteIn this class we will use JUnit 3.8.1So, when given a choice select JUnit 3CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)4Structure of a JUnit 3.8.1 Test Caseimport junit.framework.TestCase;public class FunnyIntegerSetTest01 extends TestCase {public void testInsert() {FunnyIntegerSet set = new FunnyIntegerSet ();set.insert(3);assertTrue (set != null);}public void testFindClosest() {FunnyIntegerSet set = new FunnyIntegerSet ();set.insert (3);set.insert (6);assertEquals (6, set.findClosest(5));}}Test case nameJUnit libraryNeeded (will see why later in semester)TestsAssertions (result checkers)CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)5A Test Case Is … A Class!assertion checkersassertTrue(expression);If statementis true, keep running test; otherwise, halt test, report “fail”assertFalse(expression);If statement is false, keep running test; otherwise, halt test, report “fail”assertEquals(expression1, expression2);If expression1, expression2 equal, keep running test; otherwise, halt test, report “fail”If test terminates without failing, it passes that test It continues with all susequent tests regardless of passing or failing the current test3CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)6Hints on TestingGive names to tests that relate to class being testedDevelop some tests before you codeHelps you to clarify what you are supposed to be doingGives you some ready-made tests to run while you codeUse tests to debugHow 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 falseYou should at least reach statement coverage in your own
View Full Document