Google Web Toolkit (Beta)
Search this site:

# Class GWTTestCase

public class GWTTestCase
extends TestCase
// Superclass of `Benchmark`
Acts as a bridge between the JUnit environment and the GWT environment. We hook the run method and stash the TestResult object for later communication between the test runner and the unit test shell that drives the test case inside a hosted browser.

There are two versions of this class. This version is the binary version that derives from JUnit's TestCase and handles all the work of starting up the GWT environment. The other version is a translatable class that is used within the browser. See the `translatable` subpackage for the translatable implementation.

## Methods

addCheckpoint(String) | Add a checkpoint message to the current test.
catchExceptions() | Determines whether or not exceptions will be caught by the test fixture.
clearCheckpoints() | Clears the accumulated list of checkpoint messages.
delayTestFinish(int) | Put the current test in asynchronous mode.
finishTest() | Cause this test to succeed during asynchronous mode.
getCheckpoints() | Returns the current set of checkpoint messages.
getModuleName() | Specifies a module to use when running this test case.
getTestResults() | Returns the overall test results for this unit test.
run(TestResult) | Stashes `result` so that it can be accessed during runTest().
runTest() | Runs the test via the JUnitShell environment.

## Method Detail

### addCheckpoint

public final void addCheckpoint(String msg)
Add a checkpoint message to the current test. If this test fails, all checkpoint messages will be appended to the getException description. This can be useful in web mode for determining how far test execution progressed before a failure occurs.

#### Parameters

msg
  the checkpoint message to add

### catchExceptions

public boolean catchExceptions()
Determines whether or not exceptions will be caught by the test fixture. Override this method and return `false` to let exceptions escape to the browser. This will break the normal JUnit reporting functionality, but can be useful in web mode with a JavaScript debugger to pin down where exceptions are originating.

#### Return Value

`true` for normal JUnit behavior, or `false` to disable normal JUnit getException reporting

### clearCheckpoints

public final void clearCheckpoints()
Clears the accumulated list of checkpoint messages.

#### See Also

addCheckpoint(String)

### delayTestFinish

protected final void delayTestFinish(int timeoutMillis)
Put the current test in asynchronous mode. If the test method completes normally, this test will not immediately succeed. Instead, a delay period begins. During the delay period, the test system will wait for one of three things to happen:

1. If finishTest() is called before the delay period expires, the test will succeed.
2. If any getException escapes from an event handler during the delay period, the test will error with the thrown getException.
3. If the delay period expires and neither of the above has happened, the test will error with a TimeoutException.

This method is typically used to test event driven functionality.

Example:

    public void testTimer() {
      // Setup an asynchronous event handler.
      Timer timer = new Timer() {
        public void run() {
          // do some validation logic

          // tell the test system the test is now done
          finishTest();
        }
      };

      // Set a delay period significantly longer than the
      // event is expected to take.
      delayTestFinish(500);

      // Schedule the event and return control to the test system.
      timer.schedule(100);
    }

#### Parameters

timeoutMillis
  how long to wait before the current test will time out

#### See Also

finishTest()

### finishTest

protected final void finishTest()
Cause this test to succeed during asynchronous mode. After calling delayTestFinish(int), call this method during the delay period to cause this test to succeed. This method is typically called from an event handler some time after the test method returns control to the caller.

Calling this method before the test method completes, will undo the effect of having called `delayTestFinish()`. The test will revert to normal, non-asynchronous mode.

Example:

    public void testTimer() {
      // Setup an asynchronous event handler.
      Timer timer = new Timer() {
        public void run() {
          // do some validation logic

          // tell the test system the test is now done
          finishTest();
        }
      };

      // Set a delay period significantly longer than the
      // event is expected to take.
      delayTestFinish(500);

      // Schedule the event and return control to the test system.
      timer.schedule(100);
    }

#### See Also

delayTestFinish(int)

### getCheckpoints

public final String[] getCheckpoints()
Returns the current set of checkpoint messages.

#### Return Value

a non-`null` array of checkpoint messages

#### See Also

addCheckpoint(String)

### getModuleName

public abstract String getModuleName()
Specifies a module to use when running this test case. Subclasses must return the name of a module that will cause the source for that subclass to be included.

#### Return Value

the fully qualified name of a module

### getTestResults

protected final TestResults getTestResults()
Returns the overall test results for this unit test. These TestResults are more comprehensive than JUnit's default test results, and are automatically collected by GWT's testing infrastructure.

### run

public final void run(TestResult result)
Stashes `result` so that it can be accessed during runTest().

#### Parameters

result

### runTest

protected final void runTest()
throws Throwable
Runs the test via the JUnitShell environment.