Testing Java with Tcl

TclBlend is Sun's Tcl/Java interface. We use TclBlend to test Java by writing Tcl code that exercises the classes.

This page discusses some of the details of writing Java tests with Tcl. For information about writing tests in general, see the Testing Tycho page.

Simple Example

TclBlend allows us to instantiate objects in a class and call public methods. We use TclBlend and the standard Tcl test bed to create tests. In the example below, we call java::new to create an instance of the Java NamedObj class. We can then call public methods of NamedObj by referring to the Java object handle $n:

test NamedObj-2.1 {Create a NamedObj, set the name, change it} {
    set n [java::new pt.kernel.NamedObj]
    set result1 [$n getName]
    $n setName "A Named Obj"
    set result2 [$n getName]
    list $result1 $result2
} {{} {A Named Obj}}

Java Tcl Test Files

It is best if each Java class has a separate Tcl file that contains tests. The base of the name of the Tcl test file should be the same of the Java class being tested. The Tcl test file should be located in the test subdirectory of the directory where the Java class is defined.

For example, if we are testing NamedObj.java, then the Tcl test file should be at test/NamedObj.tcl.

getJavaInfo Testing Proc

TclBlend provides a Tcl procedure called java::info which returns information about an instance of a Java class. $TYCHO/kernel/test/testDefs.tcl defines the getJavaInfo Tcl proc, which takes a Java object handle and returns a string that describes the object's fields, methods constructors, and superclass.

Each Java class should have a Tcl test that calls getJavaInfo so that if the Java class definition changes, then the test will fail. This will hopefully prompt the developer to write a test for the changed feature.

Tycho Java Infrastructure
Tycho Home Page


Copyright © 1997-1998 The Regents of the University of California. All rights reserved.
Last updated: 04/29/98, comments to: cxh@eecs.berkeley.edu