Google Code offered in: English - Español - 日本語 - 한국어 - Português - Pусский - 中文(简体) - 中文(繁體)
At this point, you've created the initial implementation of the StockWatcher application and it seems pretty stable.
As the code base evolves, how can you ensure that you don't break existing functionality? The solution is unit testing. Creating a battery of good unit test cases is an important part of ensuring the quality of your application over its lifecycle.
To aid you with your testing efforts, GWT provides integration with the open-source JUnit testing framework. You'll be able to create units test that you can run in both hosted mode and web mode.
In this section, you'll add a JUnit unit test to the StockWatcher project.
This tutorial builds on the GWT concepts and the StockWatcher application created in the Getting Started tutorial. If you have not completed the Getting Started tutorial and are familiar with basic GWT concepts, you can import the StockWatcher project as coded to this point.
File menu, select the Import... menu option.Next button.Finish button.
If you are using ant, edit the gwt.sdk property in StockWatcher/build.xml to point to where you unzipped GWT.
In order to run this tutorial, you will need to have JUnit 3 installed on your system. If you are using Eclipse, check your Eclipse plugins.
GWT junitCreator is a command-line tool that creates the files you need to begin developing JUnit tests. It generates a starter test class, scripts to run the tests from the command line, and optionally, launch configuration files for Eclipse.
For the StockWatcher project, you will run junitCreator the following parameters.
| Parameter | Definition | Example |
| -junit | The fully-qualified path name to junit.jar on your system. |
|
| -module | The GWT module for the StockWatcher application. | com.google.gwt.sample.stockwatcher.StockWatcher |
| -eclipse | The name of the Eclipse project. | StockWatcher |
| classname | The name of the test class you want to create. | com.google.gwt.sample.stockwatcher.client.StockWatcherTest |
To see the complete list of options for junitCreator, see the Developer's Guide, Command-line Tools > junitCreator.
junitCreator -junit "C:\eclipse\plugins\org.junit_3.8.2.v200706111738\junit.jar"
-module com.google.gwt.sample.stockwatcher.StockWatcher
-eclipse StockWatcher
com.google.gwt.sample.stockwatcher.client.StockWatcherTest
Created directory test Created directory test/com/google/gwt/sample/stockwatcher/client Created file test/com/google/gwt/sample/stockwatcher/client/StockWatcherTest.java Created file StockWatcherTest-hosted.launch Created file StockWatcherTest-web.launch Created file StockWatcherTest-hosted Created file StockWatcherTest-web

Take a look inside StockWatcherTest.java. This test class was generated in the com.google.gwt.sample.stockwatcher.client package under the StockWatcher/test directory. This is where you will write the unit tests for StockWatcher. Currently it contains a single, simple test: the method testSimple.
package com.google.gwt.sample.stockwatcher.client; import com.google.gwt.junit.client.GWTTestCase; /** * GWT JUnit tests must extend GWTTestCase. */ public class StockWatcherTest extends GWTTestCase { [1] /** * Must refer to a valid module that sources this class. */ public String getModuleName() { [2] return "com.google.gwt.sample.stockwatcher.StockWatcher"; } /** * Add as many tests as you like. */ public void testSimple() { [3] assertTrue(true); } }
[1] Like all GWT JUnit test cases, the StockWatcherTest class extends the GWTTestCase class in the com.google.gwt.junit.client package.
[2] The StockWatcherTest class has an abstract method (getModuleName) that must return the name of the GWT module. For StockWatcher, that is com.google.gwt.sample.stockwatcher.StockWatcher.
[3] The StockWatcherTest class is generated with one sample test case—a tautological test, testSimple. This testSimple method uses one of the many assert* functions that it inherits from the JUnit Assert class, which is an ancestor of GWTTestCase. The assertTrue(boolean) function asserts that the boolean argument passed in evaluates to true. If not, the testSimple test will fail when run in JUnit.
Before you start writing your own unit tests for StockWatcher, make sure the components of the test environment are in place. You can do that by running StockWatcherTest.java which will execute the starter test, testSimple.
You can run JUnit tests three ways:
GWT junitCreator generated two scripts: one for testing the one for testing StockWatcher in hosted mode (StockWatcherTest-hosted) and the other for testing StockWatcher in web mode (StockWatcherTest-web). If you look inside either of these scripts, you'll see that to run the tests, each launches the JUnit junit.textui.TestRunner class.
./StockWatcherTest-hostedTime: 34.554 OK (1 test)
./StockWatcherTest-webTime: 30.065 OK (1 test)
Running a compiled GWTTestCase subclass under JUnit launches an invisible instance of the Hosted Mode browser which emulates your application behavior during test execution.
You can run unit tests in Eclipse. When you pass GWT junitCreator the optional -eclipse flag, it generates Eclipse launch configurations for both hosted mode and web mode.
Run As > Open Run Dialog...StockWatcherTest-hosted-XstartOnFirstThread -Xmx256MApplyRun
Run As > Open Run Dialog...StockWatcherTest-web-XstartOnFirstThread -Xmx256MApplyRunIf you want to select the browser on which to run unit tests, use manual test mode. In manual test mode, the JUnitShell main class runs as usual on a specified GWT module, but instead of running the test immediately, it prints out a URL and waits for a browser to connect. You can manually cut and paste this URL into the browser of your choice, and the unit tests will run in that browser.
In Depth: To learn how to run unit tests in manual mode, see the Developer's Guide, Creating a Test Case.
In a real testing scenario, you would want to verify the behavior of as much of StockWatcher as possible. You could add a number of unit tests to the StockWatcherTest class. You would write each test in the form of a public method.
If you had a large number of test cases, you could organize them by grouping them into different test classes, each generated by junitCreator.
However, to learn the process of setting up JUnit tests in GWT, in this tutorial you'll write just one test and run it.
/**
* Verify that the instance fields in the StockPrice class are set correctly.
*/
public void testStockPriceCtor() {
String symbol = "XYZ";
double price = 70.0;
double change = 2.0;
double changePercent = 100.0 * change / price;
StockPrice sp = new StockPrice(symbol, price, change);
assertNotNull(sp);
assertEquals(symbol, sp.getSymbol());
assertEquals(price, sp.getPrice(), 0.001);
assertEquals(change, sp.getChange(), 0.001);
assertEquals(changePercent, sp.getChangePercent(), 0.001);
}Time: 30.065 OK (2 tests)
To see what happens when a unit test fails, you'll reintroduce the arithmetic bug you fixed in the Getting Started tutorial. Originally, the percentage of change was not calculated correctly in the getChangePercent method. To make the unit test fail, you'll break this bit of code again.
public double getChangePercent() {
return 10.0 * this.change / this.price;
}

Time: 28.704
There was 1 failure:
1) testStockPriceCtor(com.google.gwt.sample.stockwatcher.client.StockWatcherTest)junit.framework.AssertionFailedError:
expected=2.857142857142857 actual=0.2857142857142857 delta=0.0010
...
FAILURES!!!
Tests run: 2, Failures: 1, Errors: 0Time: 34.123 OK (2 tests)
Best Practices: Because there can be subtle differences between the way GWT applications work when compiled to JavaScript and when running as Java bytecode, make sure you run your unit tests in both hosted and web modes as you develop your application. Be aware, though, that if your test cases fail when running in web mode, you won't get the full stack trace that you see in hosted mode.
At this point, you've created a JUnit test for StockWatcher and added a simple test case to it.
To learn more about all kinds of unit testing in GWT, see the Developer's Guide: