我无法理解如何测试 GWT 的异步 RPC 调用。在谷歌网站上我找到了示例代码,但它并不全面:
public void testTimer() {
Timer timer = new Timer() {
public void run() {
finishTest();
}
};
delayTestFinish(500);
timer.schedule(100);
}
我必须在哪里调用远程 RPC 服务?为什么我们需要调度计时器(而不是使用 run()
方法)?
感谢您的建议。
最佳答案
您必须将 finishTest 放入 onSucess 方法中,并将测试延迟大约 500 毫秒(或者您认为需要的时间)
这是一个示例,它应该如何工作。
private void refreshWatchList() {
StockPriceService stockPriceSvc = GWT.create(StockPriceService.class);
AsyncCallback<StockPrice[]> callback = new AsyncCallback<StockPrice[]>() {
public void onFailure(Throwable caught) {
assert(false) : "The test failed because the RPC service returned an error
}
public void onSuccess(StockPrice[] result) {
//the test was a sucess so we tell the Unittest case to finish (with success)
finishTest();
}
};
// Make the call to the stock price service.
stockPriceSvc.getPrices(stocks.toArray(new String[0]), callback);
//delay the finish of the test by 500ms
//if finishTest() isn't call before 500ms passed, the test will fail
delayTestFinish(500);
}
PS:您不需要计时器来测试 RPC 调用
关于java - 如何测试GWT的RPC调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9002382/