DOC PREVIEW
UMD CMSC 131 - Lecture 18: Implicit Promotion

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

10/11/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 18:Implicit PromotionLast time:1. Implicit type promotion2. thisToday:1. Unit testing and JUnit2. Constructors revisited3. equals4. Mutable and immutable methodsCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1Project #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 automatedCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland2Unit 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 classesCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland3How To Do Unit Testing? Needs for unit testing Method for defining tests = inputs, expected outputs Method for running tests Method for reporting results One possibility: write a driver for each class 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: JUnitCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland4JUnit 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 sameCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland5Structure 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 2006Rance Cleaveland©2006 University of Maryland6A 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 libraryCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7A 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 methodCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8Example 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 inserting yield a non-empty set? etc. Does findClosest really work? 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 propertiesCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland9Running Tests in JUnit JUnit test cases are Java classes They are stored in .java files They may be manipulated like any other file To run tests using JUnit, a test runner must be specified Test runner executes each test in test case Results about passing / failing reported Installation of JUnit typically identifies test runner to be usedCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland10JUnit 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 2006Rance Cleaveland©2006 University of Maryland11JUnit and Eclipse (cont.) … select tests for which “test method stubs” should be created Click Finish Resulting .java file is …CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland12Java 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 likeCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland13Running 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 projectCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland14CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland15Hints 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


View Full Document

UMD CMSC 131 - Lecture 18: Implicit Promotion

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 18: Implicit Promotion
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 18: Implicit Promotion and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 18: Implicit Promotion 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?