29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

However, for the tester of the class it is both a benefit <strong><strong>an</strong>d</strong> a drawback. It is a benefit bec ause the<br />

encapsulated object is clearly designated for unit testing. That is, it is clear what the boundaries of the<br />

class are, i.e. <strong>an</strong>ything that is not in the class is in <strong>an</strong>other class <strong><strong>an</strong>d</strong> thus does not come into the picture<br />

(from the point of view of unit testing). It is therefore possible to test the unit in isolation.<br />

22.5.1 Black box testing<br />

Encapsulation therefore promotes black box testing. Black box testing c<strong>an</strong> be carried out in the normal<br />

way with the addition that the state of the object (<strong><strong>an</strong>d</strong> possibly t he class) must be taken into account.<br />

Black box testing (also called specification based testing) is aimed at testing a program against its<br />

specification only. That is, the class should be tested regardless of the way in which it has been coded. It<br />

is usually accomplished, even in object oriented systems, by identifying a set of messages to send to the<br />

object <strong><strong>an</strong>d</strong> the parameters to use with these messages. The results of sending these messages are then<br />

compared to the original specification (assuming one exi sts). The product of this comparison is then<br />

used to determine if the class has passed the test or not. In general, if some results are not as expected<br />

further testing would be used to determine the actual behavior of the class. A decision would then be<br />

taken to determine what action was required (i.e. modification of the source code or if appropriate,<br />

modification of the specification).<br />

22.5.2 White box testing<br />

Although encapsulation promotes black box testing, it c<strong>an</strong> make white box testing much harder. White<br />

box testing (also known as program -based testing) is complementary to black box testing. It consists of<br />

examining the internal structure of a piece of code to select test data which will exercise that code<br />

thoroughly either because that piece of code is crit ical or to gain confidence in the code to eliminate it<br />

from suspicion (for example if you are attempting to track down the source of some undesired<br />

behavior).<br />

A problem with encapsulation is that while it is entirely possible to view the source code <strong><strong>an</strong>d</strong><br />

identify the tests to perform, it is not possible to access the inst<strong>an</strong>ce variables of the object directly, nor<br />

is it usually possible to monitor the execution of the methods externally to the object.<br />

Of course in most <strong>Smalltalk</strong> environments you could use t he debugger <strong><strong>an</strong>d</strong> the inspector to examine<br />

the source code during execution (<strong><strong>an</strong>d</strong> indeed this is the intention of these tools). However, using such<br />

tools presents problems for both traceability <strong><strong>an</strong>d</strong> accountability. Thus it is not possible to trace the tests<br />

which have been performed during a project nor is it possible to record these tests <strong><strong>an</strong>d</strong> their results for<br />

later quality audits. Of course, you c<strong>an</strong> require the developer to note what they are doing, but this is<br />

nowhere near as good as having the system do it for you automatically. This approach also fails to<br />

support repeatability of tests. That is, once you have completed your testing, it is not possible to re -run<br />

the tests <strong><strong>an</strong>d</strong> check the results following some ch<strong>an</strong>ge to the class.<br />

22.5.3 Overcoming encapsulation during testing<br />

In <strong>Smalltalk</strong> it is possible to work around the problems imposed by encapsulation in a number of ways:<br />

22.5.3.1 Use of the halt message<br />

<strong>Smalltalk</strong> provides a halt message which c<strong>an</strong> be sent to <strong>an</strong> object. This will cause the obje ct to stop<br />

executing at that point <strong><strong>an</strong>d</strong> allow the user to enter the debugger. The debugger will then allow the user<br />

to view the method that is executing as well as the state of <strong>an</strong>y inst<strong>an</strong>ce <strong><strong>an</strong>d</strong> temporary variables<br />

This may at first seem intrusive, as the de veloper is modifying the source code of the method by<br />

adding the halt message. As has already been stated this is not desirable. However, in this case, it is<br />

possible to control the method’s execution through the debugger. This me<strong>an</strong>s that the developer c<strong>an</strong><br />

ensure that the intended operation of the method remains unch<strong>an</strong>ged before attempting to identify the<br />

behavior of the method under test situations. Thus this approach may not be as intrusive as would at<br />

first appear.<br />

It is unfortunate that the debugger is so named, as it is not only useful for debugging systems, it is<br />

also a particularly useful tool for white box testing. This is because, once you have entered the<br />

debugger (e.g. by introducing a self halt message into the source code of a method) it is pos sible to<br />

186

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!