Appendix B: Unit Tests with Other Programming Languages


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):

 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 (Screenshot B.1). There is a wide choice of test runner variants available online—surely one for each taste.

Java Click To expand
Screenshot 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:


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.