










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)
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)
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)
CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)
CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)
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)
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)
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 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)
Finish, click on “Click here” z Then click Finish z Resulting .java file is …
CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)
CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)