Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
23. Method <strong><strong>an</strong>d</strong> Class Testing<br />
23.1 <strong>Introduction</strong><br />
In the last chapter we discussed the problems facing the tester of <strong>an</strong> object oriented system (<strong><strong>an</strong>d</strong> in<br />
particular a <strong>Smalltalk</strong> system). We also briefly considered some approaches which overcome these<br />
problems. In this chapter we will consider the current best practice in testing object oriented systems.<br />
23.1.1 Class <strong><strong>an</strong>d</strong> inst<strong>an</strong>ce sides<br />
As was stated in the last chapter, the basic unit of test in <strong>Smalltalk</strong> is the class. However, there are two<br />
sides to a class, one is referred to as the class side <strong><strong>an</strong>d</strong> the other the inst<strong>an</strong>ce side. The class side c<strong>an</strong> be<br />
tested directly by send ing messages to the class. However, the inst<strong>an</strong>ce side c<strong>an</strong> only be tested by<br />
creating inst<strong>an</strong>ces of the class. That is, although you define the inst<strong>an</strong>ces’ methods in the class, you<br />
must test them using <strong>an</strong> inst<strong>an</strong>ce. An import<strong>an</strong>t point to remember is that, it may be necessary to use<br />
both the class <strong><strong>an</strong>d</strong> inst<strong>an</strong>ces of the class together, to adequately test both the class side <strong><strong>an</strong>d</strong> the inst<strong>an</strong>ce<br />
side. For example, let us assume we have a class such as that illustrated in Figure 23.1.<br />
Clas side<br />
Methods:<br />
currentLinkType<br />
^XLinkType<br />
Inst<strong>an</strong>ce side<br />
Inst<strong>an</strong>ce variables<br />
myLink<br />
Methods:<br />
createLink<br />
myLink := self class currentLinkType new.<br />
Figure 23.1: Inst<strong>an</strong>ce <strong><strong>an</strong>d</strong> Class method interaction<br />
In this situation, it is possible to test the class side method currentLinkType directly by sending a<br />
message to the class <strong>an</strong> d in isolation. However, it is not possible to test the inst<strong>an</strong>ce method createLink<br />
without considering the r<strong>an</strong>ge of values which may be returned by currentLinkType. In this case, it is<br />
the class XLinkType which is returned, however, it could easily be one of a r<strong>an</strong>ge of values. In addition,<br />
the class to which the currentLinkType message is sent, depends on the class of the object in which the<br />
createLink method is executing. As the createLink method may be inherited by other classes, we c<strong>an</strong>not<br />
guar<strong>an</strong>tee which class will receive the currentLinkType message.<br />
23.1.2 Testing methods in a class<br />
Each method in a class should first be tested in isolation. However, once all the individual methods in<br />
the class have been tested, threads through the meth ods in the class should be identified. This c<strong>an</strong> be<br />
done by postulating scenarios of normal <strong><strong>an</strong>d</strong> exceptional usage (which may have been produced when<br />
the class was being designed). By tracing the result of these scenarios through the class it is possible to<br />
identify threads of execution amongst the methods in the class. An example of a thread of execution is<br />
illustrated in Figure 23.2. This thread was obtained by considering a scenario in which a person object<br />
has a birthd ay. This leads to the person object being sent the message birthday. The associated<br />
method birthday is presented below:<br />
birthday<br />
Tr<strong>an</strong>script show: 'A happy birthday to ' , self name.<br />
self incrementAge.<br />
Tr<strong>an</strong>script show: 'I am now ' , self age.<br />
190