Java Reference
In-Depth Information
starts by checking that the stock watcher
RPC
was able to run in the first place. We do
this by checking to see whether the asynchronous callback caught an exception
G
.
This arrangement is helpful in determining an incorrect test setup, in particular as it
relates to the classpath and module file (see the tip in the section on running tests). If
no exception is present, the validity checks on the
StockWatcher
object can proceed.
We check that table headers are still there
H
and then check the contents of the table
I
for values we expect to be returned from the service. In this test, we changed the
stock
GWT
example to return predictable values instead of randomly generated val-
ues. If the test calls the
GWTTestCase
finishTest
J
method before the delay period
expires, then the test succeeds.
Now that the
Timer
object is in place, we call
delayTestFinish
to tell
GWT
to
run this test in asynchronous mode . You give the method a delay period in milli-
seconds much longer than what is expected to run the test setup, do the
RPC
, and
perform the assertions. When the test method exits normally,
GWT
doesn't mark
the test as finished; instead, the delay period starts. During the delay period,
GWT
checks for the following:
1)
If the test calls the
GWTTestCase
finishTest
method before the delay period
expires, then the test succeeds.
1
If an exception propagates to
GWT
, then the test fails with that exception.
2
If the delay period expires and neither of the previous conditions is true, then
the test fails with a
TimeoutException
.
3
The last task in the test is to get the job off and running with a call to the
Timer
's
schedule
method . The argument is a delay in milliseconds, after
which control returns to the caller.
4
1!
We just examined asynchronous testing in
GWT
; next, we show how to use junit-
Creator to create starter tests and how to run the tests.
13.8.3
Creating a GWTTestCase with junitCreator
The junitCreator utility allows you to create a
GWTTestCase
based on a module to
which you then add your own test methods. junitCreator is a good place to get started
on your first
GWT
test. For subsequent tests, you may prefer to clone a template class
or write test case classes from scratch. junitCreator also creates Eclipse launch config-
urations and command-line scripts for hosted and web modes. To get your command
processor to find junitCreator and other
GWT
programs, remember to add
GWT
to
your path. Here's a sample invocation for our example:
junitCreator -junit /java/junit4.6/junit-4.6.jar -module
com.google.gwt.sample.stockwatcher.StockWatcher -eclipse ch13-gwt-
StockWatcher com.google.gwt.sample.stockwatcher.client.StockWatcherTest
The (abbreviated) console output is as follows:
Created file
test\com\google\gwt\sample\stockwatcher\client\StockWatcherTest.java