Getting Started - Writing Tests


1. Put jester.jar in your Java class path.

2. Use Java 1.1 or higher, with Swing. Autotest was compiled with 1.1.8.

3. To make sure Jester is working, run one of the samples:

>java org.ajug.jsl.jester.tester.Tester org.ajug.jsl.jester.samples.TestContainer

This should run and report exceptions, probably due to assert failures.

4. Jester uses itself to test itself. Run the amazing full regression test with:

>java org.ajug.jsl.jester.tester.Tester org.ajug.jsl.jester.qc.FullTest 

(Not yet written as of 8/6/00 - JH)

5. Continuing in a fine tradition, let's write a classic Hello World test. Create and compile this Java class, except use your own package, such as com.company.your.package.name:

package org.ajug.jsl.jester.samples;
import org.ajug.jsl.jester.cases.TestCase;

public class HelloWorld extends TestCase {

//---------- Superclass Overrides --------------------------------
public void runTest() {
    assert("HelloWorld".equals("NotHelloWorld"));
    System.out.println("HelloWorld.runTest() - Completed");
}

} // End class

As easy as falling off a log, right mate? :-) Now run it with:

>java -nojit org.ajug.jsl.jester.tester.Tester org.ajug.jsl.jester.samples.HelloWorld

Your output should look something like:

Tester ----> PROBLEMS. Encountered 1 exceptions in 1 tests.

Hmmmmm, so the tester encountered an exception. Let's see what it was. Run it with the "-nocatch" option so the tester doesn't catch any exceptions:

>java -nojit org.ajug.jsl.jester.tester.Tester org.ajug.jsl.jester.samples.HelloWorld -nocatch

Which stops on the first exception, giving output something like:

org.ajug.jsl.jester.cases.AssertFailureException: Assert failed
   at org.ajug.jsl.jester.cases.TestCase.assert(TestCase.java:131)
   at org.ajug.jsl.jester.samples.HelloWorld.runTest(HelloWorld.java:9)
   at org.ajug.jsl.jester.tester.Tester.runOneTestNoCatch(Tester.java:137)
   at org.ajug.jsl.jester.tester.Tester.runSuite(Tester.java:126)
   at org.ajug.jsl.jester.tester.Tester.runSuite(Tester.java:104)
   at org.ajug.jsl.jester.tester.Tester.runSuite(Tester.java:96)
   at org.ajug.jsl.jester.tester.Tester.main(Tester.java:64)

The third line tells exactly where the problem occurred, in HelloWorld on line 9. You can edit your HelloWorld class so the assert is true, and see what output you get. The test should run to completion and show final results. You can also run this from your code debugger, see the exception, pick the line asserting the problem, and double click on it (or such) to go right to it.

6. That was too easy. :-) Let's see how to write a small suite of tests. Containers hold zero or more tests, while leafs hold no other tests. Here's a simple container test, without the package and import lines for concisness:

public class TestContainer extends TestCase {

//---------- Internal Fields -------------------------------------
private Vector children;

//---------- Superclass Overrides --------------------------------
public void beforeTest() {
    System.out.println("TestContainer.beforeTest() - Entered");
}
public void afterTest() {
    System.out.println("TestContainer.afterTest() - Entered");
}
public boolean isContainer() {
    return true;
}
public Enumeration getChildTests() {
    if (children == null) {
        children = new Vector();
        children.addElement(new HelloWorld());
        children.addElement(new HelloWorld());
    }
    return children.elements();
}

} // End class

A fixture is used in non-trivial tests, such as opening a database connection. The beforeTest() and afterTest() calls are useful for setting up fixtures and releasing their resources after the container's child tests are run. In the above example we aren't using a fixture, but instead do a print to show where it would occur.

The superclass has a default of false for isContainer(), so containers must override it. They must also provide the container's child tests in the method getChildTests(). This is straightforward. In the sample we use HelloWorld twice. In real testing you would use many different test classes here, some of which could also be containers.

Edit your HelloWorld to not fail. Create the above class in the same package and then run it with something like:

>java -nojit org.ajug.jsl.jester.tester.Tester org.ajug.jsl.jester.samples.TestContainer

If your HelloWorld is fixed to not fail the assert, the output should be something like the following, plus the test results:

TestContainer.beforeTest() - Entered
HelloWorld.runTest() - Completed
HelloWorld.runTest() - Completed
TestContainer.afterTest() - Entered
Tester ----> SUCCESS. No exceptions encountered in 3 tests.

This shows how child test runs are "sandwiched" between their container's beforeTest() and afterTest() methods. Continuing in this vein we could create and run an entire tree of tests.

7. That's all there is to it to get started. Next you can examine the additional samples and the full regression test for Jester itself. Good luck!!! :-)

(Later we will add an introduction to other features like command options, properties and policies)