2012-01-25 72 views
0

我不知道如何測試GWT的異步RPC調用。在谷歌網站上我發現的示例代碼,但它並不全面:如何測試GWT的RPC調用?

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

我在哪裏必須把調用遠程RPC服務?爲什麼我們需要計劃計時器(而不是使用run()方法)?

感謝您的建議。

回答

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

+0

謝謝你,但我總是不斷地檢索錯誤...測試總是擊碎onFailure處方法內。在控制檯中,我看到我總是得到404錯誤。沒有從服務器檢索到內容,而是服務器我的應用程序發送數據(從HTTP標頭可以看到)。但從正常使用的應用程序(從網絡瀏覽器),所有的工作都很出色。它可以檢索和發送數據。 – MyTitle

+0

我寫過我的RPC代碼,不想在我的rpc代碼中添加測試代碼。我該怎麼辦? –

+0

?你在哪裏可以看到RPC函數中的測試代碼。這僅用於RPC調用 – Stefan