24
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been fully tested and debugged

The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

Embed Size (px)

Citation preview

Page 1: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

The Fundamental Rule for Testing Methods

• Every method should be tested in a program in which every other method in the testing program has already been fully tested and debugged

Page 2: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

Pitfall: Use of the Terms "Parameter" and "Argument"

• Do not be surprised to find that people often use the terms parameter and argument interchangeably

• When you see these terms, you may have to determine their exact meaning from context

Page 3: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

The this Parameter

• All instance variables are understood to have <the calling object>. in front of them

• If an explicit name for the calling object is needed, the keyword this can be used– myInstanceVariable always means and is

always interchangeable with this.myInstanceVariable

Page 4: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

The this Parameter

• this must be used if a parameter or other local variable with the same name is used in the method– Otherwise, all instances of the variable

name will be interpreted as local

int someVariable = this.someVariable

Page 5: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

The this Parameter

• The this parameter is a kind of hidden parameter

• Even though it does not appear on the parameter list of a method, it is still a parameter

• When a method is invoked, the calling object is automatically plugged in for this

Page 6: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

A Constructor Has a this Parameter

• Like any ordinary method, every constructor has a this parameter

• The this parameter can be used explicitly, but is more often understood to be there than written down

• The first action taken by a constructor is to automatically create an object with instance variables

• Then within the definition of a constructor, the this parameter refers to the object created by the constructor

Page 7: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

Methods That Return a Boolean Value

• An invocation of a method that returns a value of type boolean returns either true or false

• Therefore, it is common practice to use an invocation of such a method to control statements and loops where a Boolean expression is expected– if-else statements, while loops, etc.

Page 8: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

The methods equals and toString

• Java expects certain methods, such as equals and toString, to be in all, or almost all, classes

• The purpose of equals, a boolean valued method, is to compare two objects of the class to see if they satisfy the notion of "being equal"– Note: You cannot use == to compare objects public boolean equals(ClassName objectName)

• The purpose of the toString method is to return a String value that represents the data in the object public String toString()

Page 9: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

Encapsulation

Page 10: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

Preconditions and Postconditions

• The precondition of a method states what is assumed to be true when the method is called

• The postcondition of a method states what will be true after the method is executed, as long as the precondition holds

• It is a good practice to always think in terms of preconditions and postconditions when designing a method, and when writing the method comment

Page 11: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

Variables in Memory

Page 12: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

References

• Every variable is implemented as a location in computer memory

• When the variable is a primitive type, the value of the variable is stored in the memory location assigned to the variable– Each primitive type always require the same

amount of memory to store its values

Page 13: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

References

• When the variable is a class type, only the memory address (or reference) where its object is located is stored in the memory location assigned to the variable– The object named by the variable is stored in some other

location in memory– Like primitives, the value of a class variable is a fixed size– Unlike primitives, the value of a class variable is a memory

address or reference – The object, whose address is stored in the variable, can

be of any size

Page 14: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

References

• Two reference variables can contain the same reference, and therefore name the same object– The assignment operator sets the reference

(memory address) of one class type variable equal to that of another

– Any change to the object named by one of theses variables will produce a change to the object named by the other variable, since they are the same objectvariable2 = variable1;

Page 15: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

Class Type Variables Store a Reference (Part 1 of 2)

Page 16: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

Class Type Variables Store a Reference (Part 2 of 2)

Page 17: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

Class Parameters

• All parameters in Java are call-by-value parameters– A parameter is a local variable that is set equal

to the value of its argument– Therefore, any change to the value of the

parameter cannot change the value of its argument

• Class type parameters appear to behave differently from primitive type parameters– They appear to behave in a way similar to

parameters in languages that have the call-by-reference parameter passing mechanism

Page 18: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

Class Parameters

• The value plugged into a class type parameter is a reference (memory address)– Therefore, the parameter becomes another

name for the argument– Any change made to the object named by the

parameter (i.e., changes made to the values of its instance variables) will be made to the object named by the argument, because they are the same object

– Note that, because it still is a call-by-value parameter, any change made to the class type parameter itself (i.e., its address) will not change its argument (the reference or memory address)

Page 19: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

Parameters of a Class Type

Page 20: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

Memory Picture for Display 5.14 (Part 1 of 3)

Page 21: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

Memory Picture for Display 5.14 (Part 2 of 3)

Page 22: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

Memory Picture for Display 5.14 (Part 3 of 3)

Page 23: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

Differences Between Primitive and Class-Type Parameters

• A method cannot change the value of a variable of a primitive type that is an argument to the method

• In contrast, a method can change the values of the instance variables of a class type that is an argument to the method

Page 24: The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been

Pitfall: Use of = and == with Variables of a Class Type

• Used with variables of a class type, the assignment operator (=) produces two variables that name the same object– This is very different from how it behaves with primitive

type variables• The test for equality (==) also behaves differently

for class type variables– The == operator only checks that two class type variables

have the same memory address– Unlike the equals method, it does not check that their

instance variables have the same values– Two objects in two different locations whose instance

variables have exactly the same values would still test as being "not equal"