Docsity

Lecture Notes on This and Unit Testing | CMSC 131, Study notes of Computer Science

Partial preview of the text

Download Lecture Notes on This and Unit Testing | CMSC 131 and more Study notes Computer Science in PDF only on Docsity!

Lecture 17: CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

this and unit testing Last time: 1. Method overloading Today: 1.^ this 2. Unit testing and JUnit

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

this z^ this: keyword for referring to current object z^ Example^ z^ Recall definition of setName method in Student class:^ public void

setName (String newName) { name^ =^ newName; } z This is equivalent to: public^ void

setName

(String

newName)

{

this.name

=^ newName; }

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

Why^ this

public class^ Computer {^ private long^

clockSpeed; private String manufacturer; public^ Computer(String manufacturer

, long^ clockSpeed) { this. manufacturer = manufacturer; this. clockSpeed = clockSpeed; } … public^ String performPsychEvaluation(Student s) { if^ (s.getName().equals("Bonnie")) {^ return^ "totally crazy"; }^ else^ {^ return^ "not as crazy as Bonnie"; } } Now add two lines to the “doAProject” student method: Computer c =^ new

Computer("Mac", 273847239847L); c.performPsychEvaluation(

this ); Note that “this” is the only way to refer to the current object above!!

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

Unit Testing z^ So far projects have consisted of a singleclass and a driver z^ Java programs typically contain manyclasses z^ Locating errors can be tricky if multipleclasses involved z^ Unit testing helps overcome this problem^ z^ Unit testing: test each class (unit) individually^ z^ Goal is to eliminate errors within classes

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

JUnit z^ A unit-testing tool for Java z^ Includes capabilities for:^ z^ Test definition, including output checking^ z^ Test running (execution)^ z^ Result reporting z^ Seamless integration with Eclipse z^ Note^ z^ In this class we will use JUnit 3.8.1^ z^ A newer version, JUnit 4.0, has recently been released, butnot yet evaluated by the CS dept.^ z^ JUnit 4.0 includes more features than JUnit 3.8.1, but basicprinciples are the same

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

Structure of a JUnit 3.8.1 TestCase import^ junit.framework.TestCase; public class

FunnyIntegerSetTest

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)); } }

JUnit library Test case name

Needed (will see whylater in semester)^ Tests Assertions (result checkers)

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

A Test Case Is … A Collectionof Tests z^ In example, a class

FunnyIntegerSet

is being tested by the test case

z^ Terminology: SUT = “software under test” z Test case methods beginning with

test^ are viewed as individual tests that the SUT either passes or fails z Passing / failing determined by

assert^ calls z^ assertTrue(b)^ If^ b^ is true, keep running test; otherwise, halt test, report “fail” z^ assertFalse(b)^ If^ b^ is false, keep running test; otherwise, halt test, report “fail” z^ assertEquals (expected, actual)^ If^ expected

,^ actual^ equal, keep running test; otherwise, halt test, report “fail” z If test terminates without failing, it passes z Test may create objects, call other methods, etc., just like any other method

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

Example Revisited z^ FunnyIntegerSet

is supposed to implement two operations on sets of integers z^ public^

void^ insert

(int^ i); add^ i^ into set z public^ int

findClosest

(int^ i); return element of set closest to

i z^ What to test?^ z^ Does insert “really work”?^ z^ Does inserting yield a non-empty set?^ z^ etc.^ z^ Does findClosest really work?^ z^ Does it return the closest value in a two-member set?^ z^ Does it return the only member of the set in a one-member set?^ z^ etc. z^ FunnyIntegerSetTest

tests two of these properties

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

JUnit and Eclipse z^ JUnit test cases may becreated in Eclipse^ z^ Right click on the module(class) you are testing.^ z^ Select New

→^ JUnit Test Case z You may be prompted toinstall JUnit if it is notalready installed z Click Next and …

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

JUnit and Eclipse (cont.) z^ It puts in the name foryou, but you may have totype in a new name ifyou are adding lots ofJUnit tests. z^ Before clicking

Finish, click on “Click here” z Then click Finish z Resulting .java file is …

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

Running Tests in Eclipse z^ Right-click in Package Explorer z^ Select Run As -> JUnit Test inside project z^ New panel appears showing results ofrunning all tests in same project

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)