com.javaranch.common
Class UnitTest

java.lang.Object
  |
  +--com.javaranch.common.UnitTest

public class UnitTest
extends java.lang.Object

Stand alone program to execute all unitTest() methods in a com tree.

This program will scan a com tree for all class files. Every class file will be loaded. If the class file is not an interface or an anonymous class, the unitTest() method will be invoked. If the unitTest() method is not found, it will be reported.

PREPARING FILES FOR UNIT TESTING

A proper unitTest() method will have the following header:

The TestLog object can be found in com.javaranch.common.

Most tests will look something like this:

e.g.

In this case, if s.length() does not result in 14 a unit test log message will be generated that looks something like this:

This example shows the time, date, source file name (com.javaranch.common.Str), the line of the file (2132), the value actually generated (13) and the value that was expected (14).

You can also specify an additional log message if you like:

Of course, the message will only be shown if the test fails.

Your computation and expected result values must be of the same type. They can be any primitive except boolean. Objects are okay too as long as they properly implement the equals() and toString() methods.

Booleans have their own slightly different methods:

If the computation results in a false, a unit test log message will be generated.

If you are performing a unit test that doesn't simply boil down to a boolean or comparison, you can use these methods:

The first will generate a unit test log message like this:

If you pass "Too many pancakes" to the second method, it will generate a unit test log message like this:

RUNNING THIS PROGRAM

If you activate this program without passing any parameters, it will try to find the com tree in the current directory. If it cannot find the com tree, it will try to find class files in the current directory.

You can pass a parameter that represents the directory where the com tree can be found. If a directory is not found, this program will search for classes in that directory.

A parameter can specify an exact file, in which case only that one file is processed. Wildcards are not accepted.

Here are some examples assuming that the JavaRanch common classes com tree is in the C:\java directory on a windows system. The first part of each example is the Windows console prompt showing the current directory. It is also assumed that the classpath is set to C:\java.

Any of these three examples would test one class file, com.javaranch.common.Str. If you were to create a file called unittest.bat that contained this one line:

and put it somewhere that your Windows PATH environment variable references, you could then use the following commands:

To test all of the classes in the com.javaranch.common package:

To test all of the classes in the com tree (including com.javaranch.common and, say, com.sun.common):

This program will have an exit value of how many failures there were. An exit value of zero means that all unit tests were successful.

Suggested practices:

- - - - - - - - - - - - - - - - -

Copyright (c) 1999-2001 Paul Wheaton

Copyright (c) 1999-2000 EarthWatch, Inc. You are welcome to do whatever you want to with this source file provided that you maintain this comment fragment (between the dashed lines). Modify it, change the package name, change the class name ... personal or business use ... sell it, share it ... add a copyright for the portions you add ...

My goal in giving this away and maintaining the copyright is to hopefully direct developers back to JavaRanch.

I originally developed this class while working as a contractor at EarthWatch, Inc. in Longmont, Colorado. They gave me permission to distribute this code this way provided that their message would also be carried along. Their message is that they hire Java programmers and would like you to consider working with them. I have to say that my experience with them was first rate and I would encourage engineers to work there. Check out their web site at http://www.digitalglobe.com.

The original source can be found at JavaRanch

- - - - - - - - - - - - - - - - -

Author:
Paul Wheaton

Constructor Summary
UnitTest()
           
 
Method Summary
static void main(java.lang.String[] args)
           
static void unitTest(TestLog log)
          Used for testing this class, do not call directly.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UnitTest

public UnitTest()
Method Detail

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception

unitTest

public static void unitTest(TestLog log)
Used for testing this class, do not call directly.