Appendix B: Unit Tests with Other Programming Languages


B.1 Smalltalk

The dispute between Java advocates and Smalltalk supporters on the benefits and drawbacks of static versus dynamic typing will probably never come to an end. However, it is uncontested that a large number of ideas and novelties originated from the Smalltalk environment, including XP and the xUnit testing framework. For this reason, it is not surprising that the test-first culture is widely used in the Smalltalk world and that SUnit—the predecessor of JUnit—is widely supported.

SUnit has meanwhile become a camp Smalltalk project; that is, it is revised and expanded in regular intervals. Version 3.0 is available for most Smalltalk environments. More than a dozen variants, including variants for Dolphin, Gemstone, Squeak, VisualAge for Smalltalk, and VisualWorks, can be downloaded from [URL:SUnit]. All of these variants are based on the same code base; only the graphical test runners are specific to a particular dialect.

Creating Test Cases with SUnit

Basically, creating test cases is similar to JUnit. Test case classes are derived from TestCase; setUp and tearDown are available, and all parameterless methods beginning with "test" are automatically grouped into a test suite.

Let's look at a simple test case that tests for adding two elements to a list (OrderedCollection):

 testTwoElements    |list|    list := OrderedCollection new.    list add: 'first'.    list add: 'second'.    self assert: list size = 2.    self assert: (list at: 1) = 'first'.    self assert: (list at: 2) = 'second'. 

The test runner that comes with the product (this example uses the version for VisualAge for Smalltalk) is unpretentious but absolutely sufficient in terms of functions (Figure B.1). There is a wide choice of test runner variants available online—surely one for each taste.

click to expand
Figure B.1: The SUnit test runner.

Differences to Java

Do test cases in SUnit look like those in JUnit? The answer can be, largely Yes, because the basic testing approach is the same in both languages. However, there are a few differences:

  • Smalltalk's lack of static typing reduces the required code quantity, in test methods also, but this may be compensated for by tests for the correct type of an object.

  • The mightier reflection mechanism and easy access to classes of the development environment open up additional automation options. For example, when the test runner encounters a failure or error, it offers you a way to let the debugger or class browser jump directly to that position within the code.

  • Smalltalk lets you create dummy and mock objects with little effort, because you don't have to implement all methods of an interface— only the ones used. The simplest dummy objects in Smalltalk are blocks.

  • Because all methods are public in Smalltalk, you don't have to use all kinds of tricks to test them.

  • Since classes are real objects, living in an inheritance hierarchy in parallel to the objects, it is easy to implement a large number of features (e.g., parametrized test suites).

  • So-called "programming in the debugger" is commonly used in Small-talk: you set break points in methods you haven't implemented yet and then start the tests and complete the actual functionality in the debugger. This allows you to work comfortably, especially in test-first development.

Evaluation

It doesn't come as a surprise that the test-first approach found its supporters initially in the Smalltalk world. Working in the mighty Smalltalk environments for a while makes you feel that you are "somehow slowed down" in Java. It seems that the large number of detours required in Java strikes you even more. What a pity that Smalltalk still has not found its way out of its niche even after 20 years. A few weeks (or more) of Smalltalk experience would be a good thing for Java developers who try to rid themselves of the usual preconceived opinions.




Unit Testing in Java. How Tests Drive the Code
Unit Testing in Java: How Tests Drive the Code (The Morgan Kaufmann Series in Software Engineering and Programming)
ISBN: 1558608680
EAN: 2147483647
Year: 2003
Pages: 144
Authors: Johannes Link

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net