Populating Test Data for GWT Tests

Sometimes you need to test GWT code that expects to find data in the data store. This approach might work:
In gwtSetUp() create the data (someData) you want to persist and call persist(someData) which would look like the following:
/** Helper method to put data into data store prior to test run. */
private void persist(SomeThing someData) {
SomeServiceAsync someSvc = GWT.create(SomeService.class);
AsyncCallback
public void onFailure(Throwable caught) {
fail("unable to store test data");
}
public void onSuccess(Boolean success) {
if (success.equals(false)) {
fail("unable to store test data");
}
}
};
// Make the call to the test service impl, passing in the callback object.
// This is the same method you defined in the SomethingService interface
// that extends RemoteService and this method needs to be implemented in
// SomethingServiceImpl
someSvc.saveSomething(someData, callback);
// Wait for it to persist.
// Using Thread.sleep(200) would give compile errors.
// Use gwt.user.client.Timer instead.
Timer timer = new Timer() {
public void run() {
}
};
timer.schedule(200);
}
Reader Comments