Java Reference
In-Depth Information
13.8.2
Creating a GWTTestCase manually
We start our asynchronous GWT RPC example test in familiar GWT territory with list-
ing 13.14, where the refreshWatchList method performs a standard GWT RPC call.
Listing 13.14
The asynchronous RPC call
void refreshWatchList() {
// Initialize the service proxy.
if (this.stockPriceSvc == null) {
this.stockPriceSvc = GWT.create(StockPriceService.class);
}
B
// Set up the callback object.
AsyncCallback<StockPrice[]> callback =
new AsyncCallback<StockPrice[]>() {
public void onFailure(Throwable caught) {
StockWatcher.this.setLastRefreshThrowable(caught);
}
C
public void onSuccess(StockPrice[] result) {
StockWatcher.this.updateTable(result);
}
};
D
// Make the call to the stock price service.
this.stockPriceSvc.getPrices(
this.getStocks().toArray(new String[0]), callback);
}
The implementation of refreshWatchList follows the standard pattern for GWT RPC ;
the method creates a new StockPriceService instance b and defines the service call-
back. The callback defines two methods; in onFailure C we save the given exception,
and in onSuccess D , which is typed for our application model ( StockPrice[] ), we
update the application. Next, we call service's getPrices method E with input data
and our callback. The key point to remember is that the call to the getPrices method
is asynchronous, so the call to refreshWatchList is also asynchronous. Next, we test
this method with a unit test.
To create a GWT test case, you start by creating a subclass of GWTTestCase , along
the lines of listing 13.15.
E
Listing 13.15
StockWatcherTest.java—testing GWT RPC
[...]
public class StockWatcherTest extends GWTTestCase {
B
@Override
public String getModuleName() {
return "com.google.gwt.sample.stockwatcher.StockWatcher";
}
C
public void testStockPrices() {
final StockWatcher stockWatcher = new StockWatcher();
final ArrayList<String> stocks = stockWatcher.getStocks();
 
 
 
 
 
Search WWH ::




Custom Search