Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Test for fun and profit, Part 3: The XML test framework

Test your Java code with an XML-based testing framework

  • Print
  • Feedback

Page 4 of 4

<testsuite>

Groups together a set of tests into a suite.

<preamble>

Contains all the code necessary to set up a single test or an entire test suite. In it, supporting objects are created, objects are, if need be, initialized, and any necessary operations are performed.

<test>

Defines a single test. A test consists (roughly) of an action and a result; it can have a descriptive name.

<action>

Contains the operation to be tested -- typically, a constructor or a method call.

<result>

Can have numerous subsections, each checking the value of a specific type of output.

<equals>

Checks the specified values.

<constructor>

Creates an instance of the specified class.

<method>

Invokes a method on a specified bound value. See <set> below.
<field>

Reads a field on a specified bound value. See <set> below.
<set>
Binds an ID to a value. Other elements can then refer to this bound value.

<get>

Accesses a bound value.

<primitive>

Creates an instance of a primitive value.


Conclusion

I'm sure by now you'd like to get your hands on some live code. Download xaf.jar, xml4j.jar, and (if you're not using the Java 2 Platform) collections.jar. xml4j.jar contains IBM's XML parser, collections.jar contains Java 2-compatible collection classes, and xaf.jar contains a free XML framework (written by yours truly) and the testing infrastructure. Add all of the jar files to your CLASSPATH.

You'll also need something to test. Download Scanner.java and compile it, then download test.xml and test.dtd. The former contains the test definitions in the format I described above.

Go ahead and give it a try. At the command line, type:

java Test test.xml


This will load and run the tests.

You now have an understanding of the importance of performing tests, and can use the tools necessary to do so. Remember to keep testing and testability in mind at all stages of the development process -- even if you won't be the one doing the testing. If you do, you'll produce better software and your customers will love you for it.

About the author

Todd Sundsted has been writing programs since computers became available in convenient desktop models. Though originally interested in building distributed applications in C++, Todd moved on to the Java programming language when it became the obvious choice for that sort of thing. In addition to writing, Todd is an architect with ComFrame Software.
  • Print
  • Feedback

Resources