如何测试GWT的RPC调用?

时间:2012-01-25 12:02:52

标签: java unit-testing gwt

我无法理解如何测试GWT的异步RPC调用。在谷歌网站上我找到了示例代码,但它并不全面:

public void testTimer() {
  Timer timer = new Timer() {
     public void run() {
       finishTest();
    }
   };
  delayTestFinish(500);
  timer.schedule(100);
}

哪里必须调用远程RPC服务?为什么我们需要计划计时器(而不是使用run()方法)?

感谢您的建议。

1 个答案:

答案 0 :(得分:3)

你必须将finishTest放入你的onSucess方法并将测试延迟大约500ms(或者你认为需要)

这是一个如何运作的例子。

    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调用