Integration with JUnit lets you test your AJAX code almost as easily as any other Java code.
By default, tests run in
hosted mode as normal Java
bytecode in a JVM. Overriding this default behavior requires passing
arguments to the GWT shell. Arguments cannot be passed directly through the
command line, because normal command-line arguments go directly to the
JUnit runner. Instead, define the system property gwt.args
to pass arguments to GWT. For example, to run in
web mode, declare
-Dgwt.args="-web" as a JVM argument when invoking JUnit. To
get a full list of supported options, declare
-Dgwt.args="-help" (instead of running the test, help is
printed to the console).
GWT includes a handy junitCreator tool that will generate a starter test case for you, plus scripts for testing in both hosted mode and web mode. But here are the steps if you want to set it up by hand:
src directorybin directorygwt-user.jar
gwt-dev-windows.jar (or gwt-dev-linux.jar)junit.jar
com.example.foo.client.FooTest test case.
public class FooTest extends GWTTestCase {
/*
* Specifies a module to use when running this test case. The returned
* module must cause the source for this class to be included.
*
* @see com.google.gwt.junit.client.GWTTestCase#getModuleName()
*/
public String getModuleName() {
return "com.example.foo.Foo";
}
public void testStuff() {
assertTrue(2 + 2 == 4);
}
}
Create the com.example.foo.Foo module.
<!-- --> <!-- Copyright 2007 Google Inc. --> <!-- Licensed under the Apache License, Version 2.0 (the "License"); you --> <!-- may not use this file except in compliance with the License. You may --> <!-- may obtain a copy of the License at --> <!-- --> <!-- http://www.apache.org/licenses/LICENSE-2.0 --> <!-- --> <!-- Unless required by applicable law or agreed to in writing, software --> <!-- distributed under the License is distributed on an "AS IS" BASIS, --> <!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or --> <!-- implied. License for the specific language governing permissions and --> <!-- limitations under the License. --> <module> <!-- Module com.example.foo.Foo --> <!-- Standard inherit. --> <inherits name='com.google.gwt.user.User'/> <!-- implicitly includes com.example.foo.client package --> <!-- OPTIONAL STUFF FOLLOWS --> <!-- It's okay for your module to declare an entry point. --> <!-- This gets ignored when running under JUnit. --> <entry-point class='com.example.foo.FooModule'/> <!-- You can also test remote services during a JUnit run. --> <servlet path='/foo' class='com.example.foo.server.FooServiceImpl'/> </module>
TestCase API.
The two key methods are GWTTestCase.delayTestFinish(int) and
GWTTestCase.finishTest(). Calling delayTestFinish()
during a test method's execution puts that test in asynchronous mode,
which means the test will not finish when the test method returns control
to the caller. Instead, a delay period begins, which lasts the
amount of time specified in the call to delayTestFinish().
During the delay period, the test system will wait for one of three
things to happen:
finishTest() is called before the delay period
expires, the test will succeed.
The normal use pattern is to setup an event in the test method and call
delayTestFinish() with a timeout significantly longer than
the event is expected to take. The event handler validates the event and
then calls finishTest().
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);
}
benchmarkViewer is a GWT tool in the root of your GWT
installation directory that displays benchmark reports.